如何为2d vector ( vector 的 vector )创建迭代器?

最佳答案

尽管您的问题不是很清楚,但我将假设您的意思是2D vector ,即 vector 的 vector :

vector< vector<int> > vvi;

然后,您需要使用两个迭代器来遍历它,第一个迭代器是“行”的迭代器,第二个迭代器是该“行”中“列”的迭代器:
//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 ...
    }
}

10-08 06:50
查看更多