In this blog we would learn how to use 2d vector in c++. 2d vectors are vectors of vectors. 2d vectors can be considered as 2d matrix. We can access 2d vectors using row and column.

Example for how to use 2d vector in c++

#include <iostream>
#include <vector>
#include <iterator>
using namespace std;

int main()
{
    int row_size = 4;
    int col_size = 3;

    // 2d vector
    vector<vector<int> > vec(row_size,vector<int>(col_size));
    vec ={{1,2,3},
          {4,5,6},
          {8,7,6},
          {1,5,6}};

    //print 2d vectors
    for(unsigned int row=0; row < vec.size();row++)
    {
        for(unsigned int col=0; col < vec[row].size();col++)
        {
            cout << vec[row][col] << " ";
        }
        cout<<endl;
    }

    return 0;
}

How to use iterator for 2d vector

#include <iostream>
#include <vector>
#include <iterator>
using namespace std;

int main()
{
    int row_size = 4;
    int col_size = 3;

    // 2d vector
    vector<vector<int> > vec(row_size,vector<int>(col_size));
    vec ={{1,2,3},
          {4,5,6},
          {8,7,6},
          {1,5,6}};

    //print 2d vectors
    vector< vector<int> >::iterator row;
    vector<int>::iterator col;
    for (row = vec.begin(); row != vec.end(); row++) {
        for (col = row->begin(); col != row->end(); col++) {
            cout << *col <<" ";
        }
        cout << endl;
    }

    return 0;
}

How to erase a particular row of 2d vectors

Our 2d vector is as

1 2 3 
4 5 6 
8 7 6 
1 5 6 

suppose we want to erase 2nd row (4,5,6). Let us code

#include <iostream>
#include <vector>
#include <iterator>
using namespace std;

int main()
{
    int row_size = 4;
    int col_size = 3;

    // 2d vector
    vector<vector<int> > vec(row_size,vector<int>(col_size));
    vec ={{1,2,3},
          {4,5,6},
          {8,7,6},
          {1,5,6}};

    //erase 2nd row
    vec.erase(vec.begin()+1);

    //print 2d vectors
    vector< vector<int> >::iterator row;
    vector<int>::iterator col;
    for (row = vec.begin(); row != vec.end(); row++) {
        for (col = row->begin(); col != row->end(); col++) {
            cout << *col <<" ";
        }
        cout << endl;
    }

    return 0;
}

output:

1 2 3 
8 7 6 
1 5 6

How to erase a particular column of 2d vectors

#include <iostream>
#include <vector>
#include <iterator>
using namespace std;

int main()
{
    int row_size = 4;
    int col_size = 3;

    // 2d vector
    vector<vector<int> > vec(row_size,vector<int>(col_size));
    vec ={{1,2,3},
          {4,5,6},
          {8,7,6},
          {1,5,6}};

    // erase 3rd column
    unsigned columnToDelete = 2;

    for (unsigned i = 0; i < vec.size(); ++i)
    {
      if (vec[i].size() > columnToDelete)
      {
        vec[i].erase(vec[i].begin() + columnToDelete);
      }
    }


    //print 2d vectors
    vector< vector<int> >::iterator row;
    vector<int>::iterator col;
    for (row = vec.begin(); row != vec.end(); row++) {
        for (col = row->begin(); col != row->end(); col++) {
            cout << *col <<" ";
        }
        cout << endl;
    }

    return 0;
}

Output:

1 2 
4 5 
8 7 
1 5

Ref:

http://www.cplusplus.com/forum/beginner/31363/



Related Contents to follow