Reputation: 11
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);
Upvotes: 0
Views: 96
Reputation: 206737
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});
Upvotes: 6