Below is chunk of code for resizing a vector of vectors. The output produced for size of each row is printed 0? Why is this happening even though i resized each row to W;
int main() {
int H,W,i;
cin >> H,W; // H=3,W=5;
vector<vector<int> >v;
v.resize(H);
for(i=0;i<H;i++)
v[i].resize(W);
cout << v[1].size(); // Output is printed 0
}
cin >> H,W;
doesn't do what you expect. According to the C++ Operator Precedence, it's the same as (cin >> H), W;
, the 2nd expression W
does nothing in fact, so W
is not initialized at all. Any usage of it would lead to undefined behavior.
Change it to cin >> H >> W;
.
BTW: Change cout << v[1].size();
to cout << v[i].size();
.