2d vector
How do I iterate through a two dimensional vector?
To iterate through and print the elements of a single dimensional vector I use,
1 2 3
|
vector<int> a;
for(vector<int>::iterator it=a.begin();it<a.end();it++)
cout<<*it;
|
How do I do the same for a two dimensional Vector?
1 2 3 4
|
vector<vector<int>> B;
for(vector<vector<int>>::iterator it=B.begin();it<B.end();it++)
for(vector<int>::iterator it_=it.begin();it_<it.end();it_++)
cout<<*it_;
|
And, instead of < , you should compare the iterator to the end with !=
Last edited on
Or, C++11:
1 2 3 4 5
|
std::vector<std::vector<int>> v ;
for ( auto& row : v )
for ( auto element : row )
std::cout << element ;
|
Topic archived. No new replies allowed.