二维向量的迭代器

2021-12-21 00:00:00 iterator vector c++ stl

如何为二维向量(向量的向量)创建迭代器?

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

推荐答案

尽管您的问题不是很清楚,但我将假设您的意思是 2D 向量表示向量的向量:

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

相关文章