0
votes

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

2

2 Answers

3
votes

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";
1
votes

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.