# [ACCEPTED]-Multi-dimensional vector-vector

`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}};`

0

```
std::vector< std::vector< int > > a; // as Ari pointed
```

Using this for a growing matrix can become 14 complex, as the system will not guarantee 13 that all internal vectors are of the same 12 size. Whenever you grow on the second dimension 11 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 10 that is fine with you (it is not really 9 a matrix but a vector of vectors), you should 8 be fine. Else you will need to put extra 7 care to keep the second dimension stable 6 across all the vectors.

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

```
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];
}
}
```

0

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

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

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

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

```
vector<int> Row;
```

One row 1 of the matrix:

```
vector<Row> My_matrix;
```

My_matrix is a vector of rows:

```
My_matrix new_matrix(5,Row(3));
```

dribeas' suggestion is really the way to 10 go.

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

As Ari pointed, `vector< vector< int>>`

is the right way to do 5 it.

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

Just use the following method to use 2-D 5 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 4 rows * columns and initializes it with zeros 3 because we are passing a zero(0) as a second 2 argument in the constructor i.e vector < int 1 > (columns, 0).

More Related questions

We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.