Vectors of Vectors

TwentyFourSeven
TwentyFourSeven used Ask the Experts™
on
Is my misunderstanding of C++ vectors flawed ?

vector<vector<int> > myVec(10,vector<int>(8));
cout << myVec[9][8] << endl;
cout << myVec[10][8] << endl;

0
Bus error


What's up with element 10 ?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2009
Commented:
Array indexes start at 0, so you can index up to myVec[9][7]

Author

Commented:
If that is the case, why is it allowing me to access elemet 8 of the inner vector ?

Author

Commented:
vector<vector<int> > myVec(10,vector<int>(8));
cout << myVec[9][8] << endl;
0
Program exited normally.
(gdb)
Top Expert 2009

Commented:
>> If that is the case, why is it allowing me to access elemet 8 of the inner vector ?

It's not because it might work in some cases that it's not wrong.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial