0

I want to check if a key (which is a pair itself) is present in the map or not.

I am new to using map and am unable to find a function which will check for a key (which is a pair).

#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;   
}                   

I received the following error-

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);
1201ProgramAlarm
  • 32,384
  • 7
  • 42
  • 56
  • You need to search for a *pair*, not two single integers. – Konrad Rudolph Apr 08 '19 at 22:12
  • Unrelated: Do not use `#include ` ([why](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h)) and avoid using `using namespace std;` ([why](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice)). Together they reinforce teach other's worst effects and can result in truly nasty and inscrutable errors. – user4581301 Apr 08 '19 at 23:13

1 Answers1

6

When you use

it=ma.find(30,40);

the compiler does not automatically convert the arguments to a pair. You'll have to do that explicitly.

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

or a simpler version

it=ma.find({30,40});
R Sahu
  • 204,454
  • 14
  • 159
  • 270