Bil*_*nch 40
由于a map的设计方式,您需要对无序数据进行相应的搜索.
for (auto it = someMap.begin(); it != someMap.end(); ++it)
    if (it->second == someValue)
        return it->first;
Pav*_*aka 15
使用Lambda(C ++ 11及更高版本)
//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;
lub*_*bgr 11
由于尚未提及这一点:结构化绑定(自 C++17 起可用)启用了一种方便的方式来编写与Bill Lynch 的回答中描述的相同的循环,即
for (const auto& [key, value] : someMap)
    if (value == someValue)
        return key;
Pau*_*aul 10
你正在寻找的是一个Bimap,它的实现可以在Boost中找到:http://www.boost.org/doc/libs/1_36_0/libs/bimap/doc/html/index.html
小智 6
我们可以创建一个将值映射到键的 reverseMap。
喜欢,
map<key, value>::iterator it;
map<value, key> reverseMap;
for(it = originalMap.begin(); it != originalMap.end(); it++)
     reverseMap[it->second] = it->first;
这也基本上类似于线性搜索,但如果您有多个查询,它将很有用。