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>
std::map<int, std::string>
std::unordered_map<int, std::string>
boost::bimap
std::find_if
std::map<int, string>
std::map<std::string, int>
map::find
if (iter->second == value_to_find)
break;
I will search by key all the time and once or twice by value.
2 4 : , . : O(n).
O(n)
1 3 . Runtime O(log n) O(1) . 3, boost: bimap.
O(log n)
O(1)
1 boost:bimap, . boost - , , .
boost:bimap
, , , , . , std::vector .
std::vector