## resize multidimensional stl vectors c++

November 4, 2010 by awhan

many numerical software and libraries handle multidimensional arrays as a single dimension array. i don’t know why but i don’t like to write

a[i*numCols+j]

instead of

a[i][j]

so i resort to using multidimensional versions of stl containers. i wonder why it is not used.

this is how i store a matrix with 4 rows and 3 columns.

// declaration and memory allocation done together
std::vector <std::vector <double > > matrix(4, std::vector<double>(3));

that creates a vector of vector.

however one can also resize the vector later after an initial declaration. up until now i only resized one-dimensional vectors but here is how one can resize multi-dimensional vectors.

// declaration alone
std::vector <std::vector<double> > matrix;
// blah blah
//
//
// now for memory allocation
matrix.resize(4, std::vector<double>(3));

### Like this:

Like Loading...

*Related*

on September 30, 2012 at 8:49 pm |bananafishtodayThank you! I just learned about vectors today and spent a couple hours struggling with syntax to resize a 2D vector until I came upon this post. (Still stuck on some C habits… I’d been realloc-ing arrays until now.)

on October 1, 2012 at 2:09 am |awhanthank you for the comment and am glad this post was of help to you. however may i suggest that you start using a specialized library for your numerical or scientific computing work. well at the time i wrote this post i was not aware but have now learnt the advantages of keeping the matrix entries in contiguous memory locations. you reap benefits of *cache hits* and *locality of reference*. so i would not recommend storing matrices in vector or vectors any more.

for most of my work i use Gnu Scientific Library but you may also try Eigen or Armadillo. of course if you need *jagged* matrices then vector of vectors will do the job but try looking for a specialized container for your data and it will serve you well. best of luck.

on January 11, 2013 at 10:35 am |SwagatI just came across this post while searching for STL based matrix allocations. I have used matrix interfaces available with GSL, Boost and even Octave. But none of them allow you to add or delete row/column dynamically (in run time). It seems to me that dynamic resizing is possible with STL vectors. What do you say?

on January 12, 2013 at 3:26 pm |awhanyes indeed dynamic resizing is a strong feature of the STL vector container. actually the memory management burden is taken care of by the container implementation and occurs transparently behind the scenes. if you try to push_back() more elements than the size of the vector then an automatic resizing is trigerred but generally the size is increased not one by one but in bulk. GSL matrices do not have this feature but i m not sure about Boost … i haven’t used Boost that much.

now a days however i strictly use STL vector to hold a matrix. NOT a vector of a vector but a plain vector … that way all the data is contiguous and you can then pass it to various legacy C LAPACK routines. also the way i use STL vectors with GSL is to create a matrix_view of the internal data of the vector. that way i don’t have to use gsl_matrix_alloc/free yet i get all the benefits of GSL routines.

on January 15, 2013 at 1:09 pm |SwagatIf you just use a vector to represent a matrix, is it not inconvenient to convert matrix[i][j] —> vector(i*numCols+J) whenever you need to access the elements. Can you give a simple example of how do you combine std::vector and GSL matrix_view together? Please do it whenever you get time.

on January 15, 2013 at 4:11 pm |awhanyeah inconvenient in writing but super convenient in other aspects so i don’t mind that any more. it is probably a good idea to use a Matrix class. i liked Armadillo but haven’t used it in a big project. same with Eigen. but the reason i want to use GSL is because it is endorsed by GNU and i want to use it and promote is as much as possible.

A small example program using std::vector and gsl_matrix_views

#include <gsl/gsl_matrix.h>

void ones(gsl_matrix * m) {

gsl_matrix_set_all(m, 1.0); // set all matrix elements to 1.0

}

int main() {

size_t const nrows = 4;

size_t const ncols = 3;

// allocate space – effectively the same as using gsl_matrix_alloc()

std::vector<double> m(nrows*ncols, 0.0);

// create a view

// m.data() returns a reference to the data location

gsl_matrix_view mv = gsl_matrix_view_array(m.data(), nrows, ncols);

ones(&mv.matrix);

// no need to free up any thing manually i.e. no gsl_matrix_free()

return 0;

}

another big advantage is that since all containers follow RAII principles if ones() by any chance seg faults on us there is no memory leak.

gcc (GCC) 4.7.2

gsl 1.15