Click to See Complete Forum and Search --> : STLPort multimap problem or mine ?


Yves M
October 24th, 2002, 11:51 AM
It is probably my problem, but I don't see what's wrong here :/

I'm trying to delete a specifc element in a multimap based on the key and value since the multimap can store multiple entries with the same key.


#include <map>
using namespace std;

struct eqstr
{
bool operator()(const char* s1, const char* s2) const
{
return strcmp(s1, s2) == 0;
}
};

typedef multimap<const char*, int, eqstr> map_type;

void eraseElem(const map_type& Map, const char* str, const int key)
{
pair<map_type::const_iterator, map_type::const_iterator> p = Map.equal_range(str);
map_type::const_iterator it = p.first;
while ((it != p.second) && ((*it).second != key)) {
++it;
}
if (it != p.second) {
Map.erase(it); // <- this line doesn't compile
}
}

int main()
{
map_type M;
M.insert(map_type::value_type("H", 1));
M.insert(map_type::value_type("H", 2));
M.insert(map_type::value_type("C", 12));
M.insert(map_type::value_type("C", 13));
M.insert(map_type::value_type("O", 16));
M.insert(map_type::value_type("O", 17));
M.insert(map_type::value_type("O", 18));
M.insert(map_type::value_type("I", 127));

eraseElem(M, "O", 18);
return 0;
}


The error I get is :
'erase' : 3 overloads have no legal conversion for 'this' pointer

I have tried it both with const_iterator and iterator. I can't really see what is wrong, since the only condition that is documented is that it is a dereferenceable iterator in Map. Or is this iterator not in Map ?

Paul McKenzie
October 24th, 2002, 12:10 PM
Remove the "const" from your function prototype.

void eraseElem(map_type& Map, const char* str, const int key)

Regards,

Paul McKenzie

Yves M
October 24th, 2002, 12:13 PM
Thanks a lot ;)

Ups that was a silly one :p