miroslavec - 2 months ago 12x

C++ Question

How to create iterator/s for 2d vector (a vector of vectors)?

Answer

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 ...
}
}
```

Source (Stackoverflow)

Comments