Shubham Jain Shubham Jain - 1 month ago 18
C++ Question

Multiple operators in one line

vector<int> G[MAXN] ;

char isLeaf[MAXN] ;

int n,path;

for(int i = 0;i < n;i++) path -= isLeaf[i] = G[i].size() == 1 ;


// How the code in For loop works? I am confused of multiple operators here.

Answer

Due to operator precedence and associativity, the statement

path -= isLeaf[i] = G[i].size() == 1 ;

is equivalent to:

path -= (isLeaf[i] = (G[i].size() == 1));

If you divide it into multiple statements for clarity, you get:

bool temp = (G[i].size() == 1);
isLeaf[i] = temp;
path -= isLeaf[i];
Comments