如何在地图中搜索一对本身的键

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

本文关键字:搜索 地图      更新时间:2023-10-16

我想检查映射中是否存在密钥(本身就是一对(。

我是使用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});