Reputation: 11
I have a created a Map with key as string type and the associated value stored in vector. Now I have a string and need to check if the each of the character in the string is present as a key in the map.
#include <iostream>
#include <stdlib.h>
#include <vector>
#include <map>
#include <string>
using namespace std;
int main() {
map<string, vector<string>> umap;
umap["1"] = {"a","b","c"};
umap["2"] = {"d","e","f"};
string s = "23";
for(int i=0; i<s.length(); i++) {
if(umap.find(s[i]) != umap.end())
cout<<"Present"<<endl;
else
cout<<"Not Present"<<endl;
}
}
Error:
main.cpp: In function ‘int main()’:
main.cpp:15:26: error: no matching function for call to ‘std::map<std::__cxx11::basic_string<char>, std::vector<std::__cxx11::basic_string<char> > >::find(__gnu_cxx::__alloc_traits<std::allocator<char> >::value_type&)’
if(umap.find(s[i]) != umap.end())
Upvotes: 1
Views: 73
Reputation: 123440
The error is maybe a bit cryptic. Lets translate it into something human readable.
main.cpp: In function ‘int main()’:
main.cpp:15:26: error: no matching function for call to ‘std::map<std::__cxx11::basic_string<char>, std::vector<std::__cxx11::basic_string<char> > >::find(__gnu_cxx::__alloc_traits<std::allocator<char> >::value_type&)’
if(umap.find(s[i]) != umap.end())
First std::__cxx11::basic_string<char>
is a complicated way to denote a std::string
. Then __gnu_cxx::__alloc_traits<std::allocator<char> >::value_type&
is an even more complicated way to denote the return type of s[i]
which is actually just char&
. Putting this together we get
main.cpp: In function ‘int main()’:
main.cpp:15:26: error: no matching function for call to ‘std::map<std::string, std::vector<std::string> >::find(char&)’
if(umap.find(s[i]) != umap.end())
I hope now you can see that the error complains that there is no overload of find
that would take a char&
as parameter.
Instead you should pass a std::string
, eg via s.substr(i,1)
.
Upvotes: 4