I have this part of a program, where the matrix is initialised with n x n values of "true".
1 2 3 4 5 6 7 8 9 10
vector< vector<bool> > adc(n, vector<bool>(n, true));
for (short i = 0; i < n; i++)
{
for (short j = 0; j < n; j++)
{
iFILE >> t; // EDIT: ifstream iFILE ("xxxxxxx.in"); -- 't' is part of a given matrix with values '0' and '1'
adc[i][j] = t ? true : false;
}
}
The thing is that I don't get how the
1 2
iFILE >> t;
adc[i][j] = t ? true : false;
actually works. Any explanations are welcome. Thanks in advance.
PS: I know that a > b ? a : b returns 'a' if 'a' is greater than 'b' and 'b' if 'b' is greater than or equal to 'a'.
You did not show what is the type of variable t. But in any case I think that it is some scalar type.
In C/C++ there is an implicit conversion of a scalar type to the bool type. If the value of a scalar type is equal to zero then it is converted to bool false. Otherwise it is converted to bool true.
Could have been a char. Or a string. Or a longer int type.
True, indeed it could have been a char, string or long type, but still, I find it quite weird to talk about that. I mean, the posibility that someone would call a variable of type char or string to assign it to a boolean two dimensional array is about 0%. And even if I would have defined that variable t as char, as it takes values of 0 and 1, it would have been still interpreted as a numerical type by the compiler. Actually the variable t could be even declared as size_t, and still would have had the same effect in the program.
I won't develop my ideas further because it's quite a waste of time.
Thanks anyway.