Question

I have the following code within the main() function

map<int, int>::iterator myvar;

. . .

if (myvar == 1) { . . . }

but when I compile, I get this error:

error: no match for âoperator==â in âmyvar == 1â

I inherited this code from a coworker who had this compiling before, so I'm curious if I have an incorrect compiler setting or do not have a needed library / package installed on my Linux machine. Any ideas?

I've tried using const_iterator instead of iterator, but that didn't seem to be enough. I also tried adding "typename" before the declaration, and that didn't help either.

Here's my g++ line:

g++ -Wall -Werror test.cpp -o test
Was it helpful?

Solution

The code is incorrect. You cannot compare an STL iterator with an integer.

If you want to compare the key, access it with myvar->first. If you want to compare the mapped value, use myvar->second.

OTHER TIPS

What you're trying to do doesn't really make sense, an iterator isn't a value you can compare against that way. What you probably mean to do is:

for(auto it = myVar.begin(); it != myVar.end(); myVar.next()) {
    if(it == 1) {
        //do something with it
    }
}

Or if you're just concerned with the very first value:

auto val = myVar.begin();
if(val == 1) {
    //use val
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top