iterator for 2d vector

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

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

8条回答
  •  情深已故
    2020-12-04 10:59

    Suppose you have a vector like this:-

    vector > vect{{1,2,3},{4,5,6},{7,8,9}};

    Now to use iterators with 2D vectors :-

     for(auto i = vect.begin() ; ibegin() ; jend() ; j++)
            cout << *j <<" ";
         cout <<"\n";  
         //similarly you can do other things
      }
    



    Also other shorter way is

     for(auto i : vect)
      {
         for(auto j : i)
            cout << j <<" ";
         cout << "\n";
    //similarly you can do other things also.
      }
    



    Please note the way of calling variables is different in both the cases.

提交回复
热议问题