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).
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
- Converting glm::lookat matrix to quaternion and ba
- What is the correct way to declare and use a FILE
We can create a reverseMap which maps values to keys.
Like,
This also is basically like a linear search but will be useful if you have a number of queries.
What you're looking for is a Bimap, and there is an implementation of it available in Boost: http://www.boost.org/doc/libs/1_36_0/libs/bimap/doc/html/index.html
Because of how a
map
is designed, you'll need to do the equivalent of a search on unordered data.Using lambdas (C++11 and newer)