Cannot understand this syntax...

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'.
Last edited on
adc[i][j] = (t ? true : false);
Does that make it clearer?
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.

In fact you could write simply

adc[i][j] = t;

or

adc[i][j] = t != 0;
You did not show what is the type of variable t.


Actually I did mention...
iFILE >> t; // EDIT: ifstream iFILE ("xxxxxxx.in"); -- 't' is part of a given matrix with values '0' and '1'
. That can only mean the variable t could be an integer of either short or int type. In my code, short. (it takes only values 0 and 1.

Anyway, thanks to both, I got it now :)
Last edited on
iFILE >> t; basicly puts the contents of t to the file designated in the initializiation of iFILE.
iFILE >> t; basicly puts the contents of t to the file designated in the initializiation of iFILE.
Not so; this is going from the file to the variable.

That can only mean the variable t could be an integer of either short or int type. In my code, short. (it takes only values 0 and 1.
Could have been a char. Or a string. Or a longer int type.
Last edited on
@Moshops
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.

Best of wishes,
~ Raul ~
Topic archived. No new replies allowed.