Create 2d vector in C Plus Plus Programming Language

Create 2d vector in C Plus Plus Programming Language

Instructor-svgAl-Mamun Sarkar
Apr 03 , 2020

Create 2d vector in C Plus Plus Programming Language. The following code shows how to create 2d vector in C++ programming language.

 

Code:

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

int main()
{
    vector < vector < int > > v(5);
    v[0].resize(10);
    v[1].resize(5);
    v[0][1] = 500;

    cout << "Vector size is : " << v.size() << endl;

    for (int i = 0; i<v.size(); ++i) {
        for (int j = 0; j<v[i].size(); ++j) {
            cout << "I is : " << i << " J is : " << j << " Value is : " << v[i][j] << endl;
        }
    }



    return 0;
}

 

Output:

Vector size is : 5
I is : 0 J is : 0 Value is : 0
I is : 0 J is : 1 Value is : 500
I is : 0 J is : 2 Value is : 0
I is : 0 J is : 3 Value is : 0
I is : 0 J is : 4 Value is : 0
I is : 0 J is : 5 Value is : 0
I is : 0 J is : 6 Value is : 0
I is : 0 J is : 7 Value is : 0
I is : 0 J is : 8 Value is : 0
I is : 0 J is : 9 Value is : 0
I is : 1 J is : 0 Value is : 0
I is : 1 J is : 1 Value is : 0
I is : 1 J is : 2 Value is : 0
I is : 1 J is : 3 Value is : 0
I is : 1 J is : 4 Value is : 0

 

  • Share On:
  • fb
  • twitter
  • pinterest
  • instagram