How to create iterator/s for 2d vector (a vector of vectors)?
Although your question is not very clear, I'm going to assume you mean a 2D vector to mean a vector of vectors:
vector< vector<int> > vvi;
Then you need to use two iterators to traverse it, the first the iterator of the "rows", the second the iterators of the "columns" in that "row":
//assuming you have a "2D" vector vvi (vector of vector of int's) vector< vector<int> >::iterator row; vector<int>::iterator col; for (row = vvi.begin(); row != vvi.end(); row++) { for (col = row->begin(); col != row->end(); col++) { // do stuff ... } }
You can use range for statement to iterate all the elements in a two-dimensional vector.
vector< vector<int> > vec;
And let's presume you have already push_back a lot of elements into vec;
for(auto& row:vec){ for(auto& col:row){ //do something using the element col } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With