Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vector with constant size

I am looking for a C++ data type similar to std::vector but without the overhead related to dynamic resizing. The size of the container will remain constant over its lifetime. I considered using boost::array, however, that is not appropriate because it requires the size of the array to be known at compile time, which is not the case in my situation.

like image 771
D R Avatar asked Aug 10 '10 05:08

D R


2 Answers

Measure if the dynamic resizing has really any performance impact before using anything non-standard.

Tip: With vector.reserve there will never be any array-reallocation.

like image 73
Markus Kull Avatar answered Oct 13 '22 00:10

Markus Kull


There's no overhead in reallocation if you don't reallocate std::vector. So either:

  • construct the std::vector with a known size ahead (std::vector x(100))
  • call reserve(n) after construction to make sure that at least n elements can be pushed into the vector before reallocation occurs.
like image 35
Nordic Mainframe Avatar answered Oct 13 '22 01:10

Nordic Mainframe