push back for the map container

user2140285 picture user2140285 · Mar 8, 2013 · Viewed 18.9k times · Source

we got this map :

std::map <int, int> values;

would this function be the same as the push_back function of a Vector :

void PushBack(int value)
{
  values[values.size()] = value;
}

since size returns the size of the container I thought it would be correct, according to the following scenario it is : index 0 = 200 index 1 = 150 you want to push back 100, values.size() would return 2, right? so then, it would , just like the normal push_back go inside index 2, correct?

Answer

John Humphreys - w00te picture John Humphreys - w00te · Mar 8, 2013

The whole point of maps is to lookup and store data based on a key that uniquely represents that data.

If you're doing this there's no point in using a map; you should choose another data structure that more appropriately addresses the design needs of the application.