iterator for 2d vector

前端 未结 8 974
眼角桃花
眼角桃花 2020-12-04 10:32

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

相关标签:
8条回答
  • 2020-12-04 11:11

    Assuming you mean an STL iterator, and a custom container that implements a generic 2D array of objects, this is impossible. STL iterators support only increment and decrement (i.e. "next" an "previous") operations, where motion through a 2D set requires four such primitives (e.g. left/right/up/down, etc...). The metaphors don't match.

    What are you trying to do?

    0 讨论(0)
  • 2020-12-04 11:16

    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 ...
        }
    }
    
    0 讨论(0)
提交回复
热议问题