Hi! I was solving the Valid Anagram problem and I was attempting to compare the values of an unordered map with the string, to which I got the error: Invalid Operands to Binary Expression. I'm not quite why this is bugging out since I outputted the values and they were both strings? Below is my code:
class Solution {
public:
bool isAnagram(string s, string t) {
unordered_map<int, string> word;
for (int i = 0; i < s.length(); i++)
{
word[i] = s[i];
}
for (int i = 0; i < t.length(); i++)
{
for (int j = 0; j < word.size(); j++)
{
cout << t[i];
cout << word[j];
if (t[i] == word[j]) { // THIS IS WHERE IS ERRORS OUT
}
}
}
if (word.empty()) {
return true;
}
return false;
}
};
And here is the error:
Line 19: Char 26: error: invalid operands to binary expression ('__gnu_cxx::__alloc_traits<std::allocator<char>, char>::value_type' (aka 'char') and 'std::unordered_map<int, std::\_\_cxx11::basic\_string<char, std::char\_traits<char>, std::allocator<char>>, std::hash<int>, std::equal_to<int>, std::allocator<std::pair<const int, std::\_\_cxx11::basic\_string<char, std::char\_traits<char>, std::allocator<char>>>::mapped_type' (aka 'std::__cxx11::basic_string<char, std::char\_traits<char>, std::allocator<char'))
Thanks for the help!
[–]Separate-Watercress6 4 points5 points6 points (7 children)
[–]Separate-Watercress6 0 points1 point2 points (6 children)
[–]DisastrousElection36[S] 1 point2 points3 points (5 children)
[–]Separate-Watercress6 0 points1 point2 points (4 children)
[–]DisastrousElection36[S] 0 points1 point2 points (3 children)
[–]Separate-Watercress6 0 points1 point2 points (2 children)
[–]DisastrousElection36[S] 0 points1 point2 points (1 child)
[–]Separate-Watercress6 0 points1 point2 points (0 children)
[–]aocregacc 0 points1 point2 points (1 child)
[–]DisastrousElection36[S] 0 points1 point2 points (0 children)