1

I am a beginner in c++. how to iterate in double dimensional vector in c++ stl?

int main()
{
    vector< vector<int>> vec;
    for(int i=0;i<vec.size();i++
    cout<<vec[i]<<" "<<endl;
}
James Z
  • 12,209
  • 10
  • 24
  • 44
  • 1
  • 1
    Does this answer your question? [Iterate through a C++ Vector using a 'for' loop](https://stackoverflow.com/questions/12702561/iterate-through-a-c-vector-using-a-for-loop) – JHBonarius Nov 21 '19 at 18:42
  • 1
    You should actually not use a vector of vectors if not necessary. You get a double indirection, as vector data is stored in heap... so there's a lot of memory access operations. It's not efficient – JHBonarius Nov 21 '19 at 18:48

4 Answers4

2

While the solution with indices is certainly right, the following variant with ranged for loops is more modern. It is a bit less flexible but for just using the values it works perfectly and has less chances for errors.

int main(){
    std::vector<std::vector<int>> vec;
    // add some data to vec
    for(const auto &v: vec){    // the & is important otherwise you copy the inner vector
        for(const auto &i: v){  
            std::cout << i << ' ';
        }
        std::cout << '\n';
    }
    return 0;
}

If you want to modify the elements, you have to get rid of the consts.

n314159
  • 4,990
  • 1
  • 5
  • 20
  • 1
    The `&` on the inner loop is still sensible. This block of code can now be reused for any Library (or Library-like) container to print any contents that have an `operator<<` overload. – user4581301 Nov 21 '19 at 18:49
  • That is true, but I think for scalar types, taking by value is actually more efficient. But of course the `&` is a good default, especially if we want to make changes and I always do it that way. – n314159 Nov 21 '19 at 18:53
  • At the end of the day a lot of it depends on how the compiler's going to optimize. For a `const` reference and a trivial copy that goes unchanged, the behaviour's the same, so a good compiler will probably crank out the same output. – user4581301 Nov 21 '19 at 19:42
1

You can iterate like this,

int main()
{
    vector< vector<int>> vec;
    for(int i=0;i<vec.size();i++)
    {
        for(int j=0;j<vec[i].size();j++)
           cout<<vec[i][j]<<" ";
        cout<<endl;
    }
}
Ratnesh
  • 1,554
  • 3
  • 12
  • 28
  • 1
    You are missing a closing `)` at the first `for`. And I would recommend to at least indent the inner body of the second `for` if you dont want to put it in an own block. – n314159 Nov 21 '19 at 18:40
1

You can use Range-based for loop like this

std::vector<std::vector<int>> vecOFvec{{1,2},{3,4,5},{6,7,8,9}};

for(const auto& elemOuter:vecOFvec){
    std::cout<<"\n";
    for(const auto& elemInner:elemOuter)
                std::cout<<elemInner<<" ";

}

Output

1 2 
3 4 5 
6 7 8 9
nae9on
  • 249
  • 3
  • 9
0

Lets you have a 2D Mattrix in Array

int matt[R][C];

Iterating the 2D Array

for(int r=0; r < R; r++){
   for(int c=0; c<C;c++)
       cout << matt[r][c];
   cout << endl;
}

Similarly for 2D vector, you first need the number of rows

We get that by vec.size();

Then we need the column size

we get that by vec[0].size() or vec[i].size()

This simply means the size of the column corresponding to the 0th or ith row

for(int i=0; i< vec.size(); i++){
    for(int j=0; j<vec[i].size(); j++)
        cout << vec[i][j] << " ";
    cout << endl;
}

You can use an iterator to iterate through the vector but remember Iterator stores snapshot the vector/array and the beginning of the iteration. If the vector changes its size during for loop you might face some problems

Hrishikesh
  • 356
  • 2
  • 11