I have a collection of objects, each of which has an identifier (string). Now I wonder whether I should store them as a vector or a map, so either std::vector<cObject>
or std::map<std::string,cObject>
. Each name can only be used once, so I need to check the vector/map for occurrences of a given name. Using a map, I would use find() which scales logarithmically, while for a vector, I would iterate and test oObject.name == newname
until I find the name or arrive at the end, which scales linearly. A downside of the map is that the name is stored twice, once inside the object and once as the key.
While for large vectors/maps, a map would always win, I wonder at which point this is the case, since a map seems to be overkill if I only have up to 10 objects to store.
This leads to my question: At which point does a map become advantageous (regarding performance) compared to a plain vector? Should I already consider using maps if the expected number of objects is about 10, 100, 1000 or even larger? I admit that this question is rather vague, but I hope to get some advice anyway, to get a feeling when to use which container.