#include<bits/stdc++.h>
using namespace std;
typedef long long LL ;
vector <LL> v[100];
vector <LL> v1;
int main(){
cout << v[1].size() - 1 << endl;
//This gives output 4294967295
int sz = v[1].size() - 1;
cout << sz << endl;
//This gives output -1
LL sz1 = v[1].size() - 1;
cout << sz1 << endl;
//This gives output 4294967295
cout << v[1].size() << endl;
//This gives output 0
cout << v1.size() - 1 << endl;
//This gives output 4294967295
cout << v1.size() << endl;
//This gives output 0
}
Can anyone give any explanation? I lost a lot of valuable time in the contests for these stuffs and I can't understand why.
The vector's (and other standard data structures like stack, set, ...)
.size()
is of typeunsigned int
, which has limits of 0 to 232 - 1.Like int (a normal
int
has its limits from - 231 to 231 - 1), if you give it a value that exceeds its range, it will just cycle it around to a value that is in the range. For example, giving an unsigned int the value - 10 would make it cycle up to 232 - 10.So, when you give the unsigned the value - 1, it becomes 4294967295, and then if you assign this value to an int it cycles the int down, and gets it to - 1. If you assign this value to a
long long
type, no cycles occur because the value is inside long long's range.Thanks i got it.