Find on a map by value - preference for methods in terms of speed

Four methods come to my mind to find in the map by value. What will be the order of preference of the methods in terms of speed?

Assuming that the key and value are unique among themselves.

std::map<int, std::string> or std::unordered_map<int, std::string>

  • Use boost::bimapif boost library is available.
  • Use std::find_ifwith predicate / functor with some equality comparison logic
  • While filling, std::map<int, string>create and fill in another one std::map<std::string, int>and execute map::findon the second map.
  • Iterate over the entire map and check if (iter->second == value_to_find)if foundbreak;

I will search by key all the time and once or twice by value.

+3
source share
1 answer

2 4 : , . : O(n).

1 3 . Runtime O(log n) O(1) . 3, boost: bimap.

1 boost:bimap, . boost - , , .

, , , , . , std::vector .

+3

All Articles