Reputation: 455
so I try to compare iterator value that I get from it->second with int to get the key from map on the base of the value. I have this code :
std::map<string, std::vector<int> >::const_iterator it;
void getStudentByGrade(int gradeComp)
{
for (it = studMap.begin(); it != studMap.end(); it++)
{
if (it->second == gradeComp)
{
cout << it->first;
}
}
}
and in the if statement I get the error , but I compare two ints don't I ?? The int that is passed as gradeComp and the it->second.
The error is:
Severity Code Description Project File Line Suppression State Error C2678 binary '==': no operator found which takes a left-hand operand of type 'const std::vector>' (or there is no acceptable conversion)
How to solve the error ? I found examples of getting to key with value only with iterator.
Upvotes: 2
Views: 159
Reputation: 40060
I compare two ints don't I??
No you do not. Look:
it->second == gradeComp
Since it
is an (const) iterator to std::map<string, std::vector<int>>
, it->second
has type std::vector<int> const&
. On the other hand, gradeComp
is an int
. This is why your compiler tells you
No operator “==” maches these operands
those operands being a vector of integers and an integer.
How to solve the error?
Well, it depends on what you mean. Do you mean to search gradeComp
in the vector? Or maybe you mean to compare it with a specific value from the vector?
// is gradeComp contained in the found vector?
if (end(it->second) != std::find(begin(it->second), end(it->second), gradeComp))
{ /* ... */ }
// does gradeComp match n-th value of vector?
if (gradeComp == it->second[n])
{ /* ... */ }
Upvotes: 6