I assume that you are comparing map<A, B> with vector<pair<A, B> > .
Firstly, finding an element in a very small vector can easily be faster than the same thing in the map, because all the memory in the vector is always contiguous (and therefore plays more beautifully with computer caches, etc.), and the number of comparisons required to search for something in the vector may be about the same as for the map. Finding an item on a map requires fewer operations within very large containers.
The point where cards become faster than vectors depends on the implementation, on your processor, on the data on the card, and on subtle things like memory in the processor cache. As a rule, the point where the map becomes faster will be about 5-30 elements.
An alternative is to use a hash container. They are often called hash_map or unordered_map . Classes named hash_map not part of the official standard (and there are several options); std::tr1::unordered_map is. A hash map is often faster than a regular search map, no matter how many elements it contains, but whether it depends on speed, it depends on what the key is, how it is hashed, what values โโyou have in mind and how the key is compared in std :: map. It does not save things in a specific order, like std :: map, but you said that you do not care. I would recommend hash maps, especially if the keys are integer or index, because these hashes are very fast.
Doug Jan 18 '09 at 7:45 2009-01-18 07:45
source share