32

How can I create a 2D vector? I know that in 2D array, I can express it like:

a[0][1]=98;
a[0][2]=95;
a[0][3]=99;
a[0][4]=910;

a[1][0]=98;
a[1][1]=989;
a[1][2]=981;
a[1][3]=987;

How can one do this using the C++ STL Vector?

Reinderien
  • 11,755
  • 5
  • 49
  • 77

8 Answers8

32

vector<vector<int> > a;

If you want to define the rows and columns,

vector<vector<int> > a{{11, 2, 4}, {4, 5, 6}, {10, 8, -12}};

csg
  • 8,096
  • 3
  • 14
  • 38
Ari
  • 3,460
  • 3
  • 24
  • 31
30
std::vector< std::vector< int > > a; // as Ari pointed

Using this for a growing matrix can become complex, as the system will not guarantee that all internal vectors are of the same size. Whenever you grow on the second dimension you will have to explicitly grow all vectors.

// grow twice in the first dimension
a.push_back( vector<int>() );
a.push_back( vector<int>() );

a[0].push_back( 5 ); // a[0].size() == 1, a[1].size()==0

If that is fine with you (it is not really a matrix but a vector of vectors), you should be fine. Else you will need to put extra care to keep the second dimension stable across all the vectors.

If you are planing on a fixed size matrix, then you should consider encapsulating in a class and overriding operator() instead of providing the double array syntax. Read the C++ FAQ regarding this here

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
David Rodríguez - dribeas
  • 204,818
  • 23
  • 294
  • 489
13
std::vector< std::vector<int> > a;

    //m * n is the size of the matrix

    int m = 2, n = 4;
    //Grow rows by m
    a.resize(m);
    for(int i = 0 ; i < m ; ++i)
    {
        //Grow Columns by n
        a[i].resize(n);
    }
    //Now you have matrix m*n with default values

    //you can use the Matrix, now
    a[1][0]=98;
    a[1][1]=989;
    a[1][2]=981;
    a[1][3]=987;

//OR
for(i = 0 ; i < m ; ++i)
{
    for(int j = 0 ; j < n ; ++j)
    {      //modify matrix
        int x = a[i][j];
    }

}
aJ.
  • 34,624
  • 22
  • 86
  • 128
7

If you don't have to use vectors, you may want to try Boost.Multi_array. Here is a link to a short example.

Benoît
  • 16,798
  • 8
  • 46
  • 66
2

Declaration of a matrix, for example, with 5 rows and 3 columns:

vector<vector<int> > new_matrix(5,vector<int>(3));

Another way of declaration to get the same result as above:

vector<int> Row;    

One row of the matrix:

vector<Row> My_matrix;

My_matrix is a vector of rows:

My_matrix new_matrix(5,Row(3)); 
IluxaKuk
  • 389
  • 3
  • 7
0

Just use the following method to use 2-D vector.

int rows, columns;        

// . . .

vector < vector < int > > Matrix(rows, vector< int >(columns,0));

                                  Or

vector < vector < int > > Matrix;
Matrix.assign(rows, vector < int >(columns, 0));

// Do your stuff here...

This will create a Matrix of size rows * columns and initializes it with zeros because we are passing a zero(0) as a second argument in the constructor i.e vector < int > (columns, 0).

Community
  • 1
  • 1
Aditya Goel
  • 381
  • 3
  • 5
0

dribeas' suggestion is really the way to go.

Just to give a reason why you might want to go the operator() route, consider that for instance if your data is sparse you can lay it out differently to save space internally and operator() hides that internal implementation issue from your end user giving you better encapsulation and allowing you to make space or speed improving changes to the internal layout later on without breaking your interface.

Robert S. Barnes
  • 39,711
  • 30
  • 131
  • 179
0

As Ari pointed, vector< vector< int>> is the right way to do it.

In addition to that, in such cases I always consider wrapping the inner vector (actually, whatever it represents) in a class, because complex STL structures tend to become clumsy and confusing.

csg
  • 8,096
  • 3
  • 14
  • 38
Igor
  • 26,650
  • 27
  • 89
  • 114