Honza
Honza

Reputation: 1934

C++: Vector of fixed but runtime defined length arrays

I know that I can use

std::vector<std::array<int, 20> > myVec;

as a container for a matrix growable in single dimension. However std:array<> size has to be defined in compile time. Is there any simple way how to do the same for runtime defined arrays?

I need this for processing several GB of data so I'm affraid that vector of vectors would bring in unnecessary overhead.

Upvotes: 3

Views: 381

Answers (2)

Ivan
Ivan

Reputation: 2057

You should consider using std::vector< std::vector<int> > to hold your data. Premature optimization is not a good thing to do.

If it is too slow, you can write your own or use a third-party memory allocator for that vector. Custom memory allocators could be significantly faster for specific aims like yours (storing many objects of the same size).

The idea of Loopunroller of using wrapper around std::vector<int> is nice too.

Upvotes: 3

Columbo
Columbo

Reputation: 60969

I need this for processing several GB of data so I'm affraid that vector of vectors would bring in unnecessary overhead.

Then write a wrapper class around a one dimensional vector which allows two-dimensional indexing, and give the one fixed dimension as a constructor-argument.

That is the most efficient way, since there is only one level of indirection.

Upvotes: 7

Related Questions