I'm trying to have a map of <string, int>. I am trying to put individual characters of a given string as the key of the key value pair. However, I am running into this error:
Line 7: no matching function for call to 'std::unordered_map<std::__cxx11::basic_string<char>, int>::find(__gnu_cxx::__alloc_traits<std::allocator<char> >::value_type&)'
This is my code:
int lengthOfLongestSubstring(string s) {
unordered_map<string, int> map;
int count = 0;
for (int i = 0; i < s.length(); i++) {
if (map.find(s[i]) == map.end()) {
count++;
map.insert(make_pair<string, int>(s[i], 1));
} else {
map.clear();
count = 0;
}
}
return count;
}
I think the error is because s[i] becomes a char* and so I cannot do make_pair since char* and string are different types.
I have tried to get around this by doing:
string temp(s[i]); // Try to create a string from the char* and pass it into the make_pair function
However, I still get the same error.
s[i]is not achar*, it is simply achar.s[i]is achar. You can’t construct astd::stringwith just achar.mapof a single character as the key or multiple characters?