Find mapped value of map
Because of how a map
is designed, you'll need to do the equivalent of a search on unordered data.
for (auto it = someMap.begin(); it != someMap.end(); ++it)
if (it->second == someValue)
return it->first;
If you are doing this kind of search frequently in larger maps, then it may be interesting to look at a Bimap, that will index both keys and values. There is an implementation of a Bimap available in Boost: https://www.boost.org/doc/libs/1_77_0/libs/bimap/doc/html/index.html
Using lambdas (C++11 and newer)
//A MAP OBEJCT
std::map<int, int> mapObject;
//INSERT VALUES
mapObject.insert(make_pair(1, 10));
mapObject.insert(make_pair(2, 20));
mapObject.insert(make_pair(3, 30));
mapObject.insert(make_pair(4, 40));
//FIND KEY FOR BELOW VALUE
int val = 20;
auto result = std::find_if(
mapObject.begin(),
mapObject.end(),
[val](const auto& mo) {return mo.second == val; });
//RETURN VARIABLE IF FOUND
if(result != mapObject.end())
int foundkey = result->first;
Structured bindings (available since C++17) enable a convenient way of writing the same loop as depicted in Bill Lynch's answer, i.e.
for (const auto& [key, value] : someMap)
if (value == someValue)
return key;