std::map is optimized for fast searching.
It has its own find method that uses its internal structure to provide good performance.
In general, it will only inspect log(N) keys, where N is the number of items in the map.
std::list<std::pair> is a simple linked list, and so only supports element-by-element traversal.
You could use the separate std::find algorithm, or std::find_if with a custom predicate that only examines the first member to better match the semantics of std::map::find, but that would be very slow.
In fact, it will have to look at every pair in the list for any failed search, and will look at half on average for any successful one.
UPDATE: Assuming you meant the difference between map<> and list<pair<> >:
Map should be implemented for fast lookup on the key member.
list has just a simple linear list. Looking up an item in a list requires running through the whole list. However, std::find() will work on both.
浙公网安备 33010602011771号