Reputation: 21
I have a vector a={1,2}, so a.size()=2;
I found that:
cout<<(a.size()-3);
will give me a weird number (e.g. 18446744073709551615).
why is it not -1?
if I do:
cout<<(a.size-2);
or
int s = a.size-3;
cout<<s;
everything is normal (-1).
Upvotes: 2
Views: 260
Reputation: 311978
size()
returns a size_t
, which is an unsigned type. When performing a calculation that's supposed to return a negative number, it underflows, and you get the huge value you observed. When you explicitly assign the result to a (signed) int, you treat the result as a signed number, and get the expected result of -1
.
Upvotes: 1