0

How can I check if an element of a multimap exists?
With this code:

typedef std::multimap<std::string, std::string> TagVal;
TagVal tv;
//... add values to tv ...
TagVal::const_iterator it = tv.find("abc");
if(it == TagVal::end())    // <--- ERROR
    cerr << "Error";

I get the following compile time error:

error: cannot call member function 'std::multimap<...>::iterator std::multimap<...>::end() ... without object.

Platform: Linux, GCC 4.5.1

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
Pietro
  • 12,086
  • 26
  • 100
  • 193

2 Answers2

3

The reason is that end is not a static method, it has to be called on the object you got the iterator from:

if(it == tv.end())
    cerr << "Error";
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
1

since you have initialised tv as

TagVal tv;

you have to call end() function of multimap class as:

it == tv.end()

as the end() is called on that object and it is not a static method.

Prasanth Madhavan
  • 12,657
  • 15
  • 62
  • 94