C++ 查找地图的映射值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4263640/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 14:57:29  来源:igfitidea点击:

Find mapped value of map

c++dictionaryfindstdmap

提问by wrongusername

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)->secondto get the value, but here I want to do the opposite and obtain the key (the values and keys are all unique).

C++ 中有没有办法搜索映射的映射值(而不是键),然后返回键?通常,我这样做是someMap.find(someKey)->second为了获取值,但在这里我想做相反的事情并获取键(值和键都是唯一的)。

采纳答案by Bill Lynch

Because of how a mapis designed, you'll need to do the equivalent of a search on unordered data.

由于 amap的设计方式,您需要对无序数据进行等效的搜索。

for (auto it = someMap.begin(); it != someMap.end(); ++it)
    if (it->second == someValue)
        return it->first;

回答by Pavan Chandaka

Using lambdas (C++11 and newer)

使用 lambdas(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;

回答by Paul

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

您正在寻找的是 Bimap,并且在 Boost 中有它的实现:http: //www.boost.org/doc/libs/1_36_0/libs/bimap/doc/html/index.html

回答by lubgr

As this has not been mentioned yet: structured bindings (available since C++17) enable a convenient way of writing the same loop as depicted in Bill Lynch's answer, i.e.

由于尚未提及这一点:结构化绑定(自 C++17 起可用)启用了一种方便的方式来编写与Bill Lynch 的回答中描述的相同的循环,即

for (const auto& [key, value] : someMap)
    if (value == someValue)
        return key;

回答by Karan Saxena

We can create a reverseMap which maps values to keys.

我们可以创建一个将值映射到键的 reverseMap。

Like,

喜欢,

map<key, value>::iterator it;
map<value, key> reverseMap;

for(it = originalMap.begin(); it != originalMap.end(); it++)
     reverseMap[it->second] = it->first;

This also is basically like a linear search but will be useful if you have a number of queries.

这也基本上类似于线性搜索,但如果您有多个查询,它将很有用。

回答by ???

struct test_type
{
    CString str;
    int n;
};


bool Pred( std::pair< int, test_type > tt )
{
    if( tt.second.n == 10 )
        return true;

    return false;
}


std::map< int, test_type > temp_map;

for( int i = 0; i < 25; i++ )

{
    test_type tt;
    tt.str.Format( _T( "no : %d" ), i );
    tt.n = i;

    temp_map[ i ] = tt;
}

auto iter = std::find_if( temp_map.begin(), temp_map.end(), Pred );