2d Vector Printing

Should be a simple question: How does one print one element of a 2D vector? For example with an array you would use int myarray[3] = {4,3,1,2}; cout << myarray[0];

the output being: 4.

Using that on vectors does not work:

myvector[0][1] does not compile.
Using that on vectors does not work:

myvector[0][1] does not compile.


vectors are like 1 dimensional arrays myvector[0] should work.

myvector[0][1] doesn't work because that is a 2 dimension reference.

Do you want to have more than 1 vector? You could have a list of vectors.

I don't like the idea of a vector of vectors as this can be very inefficient if they have to be resized.
Yes, the vector was declared as follows:

vector< vector<string> > myvector

But when I try and use cout << myvector[0][1]; i get a subscript out of range error
Last edited on
I think you need the at function ( a member of vector)

Google C++ vector example -there are all kinds of functionality for vectors, some you may not be aware of.

As I said, I am not a big fan of vector of vectors. Consider using a different container like <list> a double linked list, or <map> or <set>. Google these.

<set> and <map> are very efficient.
Topic archived. No new replies allowed.