문제

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
도움이 되었습니까?

해결책

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>

다른 팁

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.

If you wish to have vector of fixed size, most likely you don't need one! Use std::array instead.

But still you insist to have one..

vector<vector<int> > vecOfVec(NumberOfVectors);
for ( int i = 0 ; i < NumberOfVectors; i++ )
   vecOfVec[i].resize(NumberOfElementsInVector);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top