我想检查映射中是否存在键(它本身是一对)。
我刚开始使用 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});
关于c++ - 如何在 map 中搜索本身就是一对的键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55582386/