繁体   English   中英

如何在地图中搜索本身就是一对的钥匙?

[英]How to search for a key which is a pair itself, in a map?

我想检查地图上是否存在钥匙(本身就是一对)。

我是使用Map的新手,无法找到要检查键(一对)的函数。

#include<bits/stdc++.h>
using namespace std;
#define  ll long long int
typedef pair<ll,ll> my_key_type;
typedef map<my_key_type,ll> my_map_type;
int  main()
{
    my_map_type ma;
    my_map_type::iterator it;
    ma.insert(make_pair(my_key_type(30,40),6));
    it=ma.find(30,40);
    if(it==ma.end())
    {
        cout<<"not present";
        return 0;
    }
    cout<<"present";
    return 0;   
}                   

我收到以下错误-

no matching function for call to ‘std::map<std::pair<long long int, long long int>, long long int>::find(int, int)’   it=ma.find(30,40);

使用时

it=ma.find(30,40);

编译器不会自动将参数转换为一对。 您必须明确地做到这一点。

it=ma.find(std::make_pair(30,40));

或更简单的版本

it=ma.find({30,40});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM