Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find mapped value of map

Is there a way in C++ to search for the mapped value (instead of the key) of a map, and then return the key? Usually, I do someMap.find(someKey)->second to get the value, but here I want to do the opposite and obtain the key (the values and keys are all unique).

like image 494
wrongusername Avatar asked Nov 24 '10 05:11

wrongusername


People also ask

How do you find the value of a map?

Search by value in a Map in C++ Given a set of N pairs as a (key, value) pairs in a map and an integer K, the task is to find all the keys mapped to the give value K. If there is no key value mapped to K then print “-1”. Explanation: The 3 key value that is mapped to value 3 are 1, 2, 10.

How do you find if a value exists in a map C++?

To check if a key exists in a C++ map, you can use std::map::count. It returns 0 (the key is absent) or 1 (the key is present).

How does map Find () work?

map find() function in C++ STL Return Value: The function returns an iterator or a constant iterator which refers to the position where the key is present in the map. If the key is not present in the map container, it returns an iterator or a constant iterator which refers to map.

What is map end ()?

C++ Map Library - end() Function The C++ function std::map::end() returns an iterator which points to past-the-end element in the map. The past-the-end element is the theoretical element that would follow the last element in the map.


1 Answers

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; 
like image 86
Bill Lynch Avatar answered Oct 23 '22 21:10

Bill Lynch