mardi 23 novembre 2021

how to understand the or || in c++ [duplicate]

I have an 2d vector as below.

vector<vector<int>> mat = {
        {1,1,0,1},
        {1,0,0,0},
        {0,0,0,1},
        {0,0,1,1},
        {0,1,0,0}
    };

What I want to do is check if the element is left edge or it's left element is 1.

So what I tried to do is:

for (int r = 0; r < 5; ++r){
   for (int c = 0; c <4; ++c){
       if (r - 1 < 0 || mat[r-1][c] == 1)
   }
}

My question is how is this line executed?

if (r - 1 < 0 || mat[r-1][c] == 1)

Does it caculate the r-1 and mat[r-1][c] == 1 seperately and then do or operation? In this case, there may be illegal memory access when r == 0?

Seems my code runs ok but I don't this makes sense to me.

Could someone help me with this?

Aucun commentaire:

Enregistrer un commentaire