smilingbuddha
smilingbuddha

Reputation: 14774

map of vectors C++

Hi if I C++ I created an STL map of the type map<int, vec<double>> are the vectors stored under the different keys placed consecutively in memory?

Upvotes: 0

Views: 454

Answers (2)

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272822

No.*

A map dynamically allocates elements as required. So each vector could be placed anywhere in memory (and may move over time).

Remember also that the element storage within the vector itself is also dynamically allocated. So even if you just created a straightfoward std::vector<double> my_vectors[10], the elements themsleves would not be contiguous between the individual vectors.

* An implementation could conceivably allocate from an array-backed store, but you wouldn't be able to rely on this behaviour. So it's best to assume "no".

Upvotes: 8

Tony The Lion
Tony The Lion

Reputation: 63320

Technically, that would be dependent on implementation of map, but in general, a map is implemented as a Tree, so the stores are not consecutive in memory, due to the nature of the tree datastructure.

Upvotes: 14

Related Questions