c++ - 如何在 map 中搜索本身就是一对的键?

标签 c++

我想检查映射中是否存在键(它本身是一对)。

我刚开始使用 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/

相关文章:

c++ - 访问位域中的所有空闲槽

c++ - 静态变量的位置

c++ - std::deque 的线程安全

C++ std::lower_bound() 函数查找索引排序 vector 的插入点

c++ - 如何更改类中的数组?

c++ - 为什么抽象类的默认析构函数不是虚拟的?

c++ - 我怎样才能直观地看到 RSU 的范围?

c++ - CUDA内核代码中矩阵的多次乘法

c++ - delete[] 与 for 循环中的 delete

c++ - 从 QT 复选框到 Postgresql 的星期选择