Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ vector of fixed size vecors

Tags:

c++

stl

How can I declare vector of fixed size vectors in C++?

For example:

vector of vectors with N elements.

Not this:

vector<vector<int> > v(N) //declares vector of N vectors
like image 674
torayeff Avatar asked Feb 13 '23 02:02

torayeff


2 Answers

std::array is your friend here.

http://en.cppreference.com/w/cpp/container/array

For instance, to declare vector of vectors with N elements, you can

typedef std::array<int, N> N_array;

Then use

std::vector<N_array>
like image 140
K.Chen Avatar answered Feb 15 '23 16:02

K.Chen


You could use std::array:

std::array<int, 10>    myNumbers;

The only down side to this is you can't see how many "active" elements there are, since you don't push/emplace back. You use it like an ordinary( but safe ) array.

like image 29
Ben Avatar answered Feb 15 '23 15:02

Ben