2d-vector vs 1d-vector

In C ++ 11, how is a 2D vector versus a 1D vector in time?
In the indicated two-dimensional vector, all internal vectors are the same size.

Example:

std::vector<std::vector<int>> X{10, std::vector<int>(4)};

vs

std::vector<int> Y(40);

Which vector avatar will work best when items are randomly available?

+1
source share
1 answer

The only std::vectorone inherently simpler is just an adjacent block of memory that is stored somewhere.

A std::vectorof std::vectorhas more overhead, but also more powerful (since each inner vector can have a different size, for example).

, , :

  • size_t index = x + y*WIDTH
  • , , , .
+5

All Articles