My requirement is to delete a a “value” from the multimap and not the “key”.
A key may have multiple values and i want delete a specific value.My requirement is similar to deleting a node from a linked list.
I am doing so by using multimap::erase() method.
But after deletion if I try to print the values of the multimap, the values deleted using multimap::erase() are also printed.
below is my code snippet:
void Clientqueues::clearSubscription(string name,string sessionid)
{
pair<multimap<string,string>::iterator,multimap<string,string>::iterator> i;
multimap<string, string>::iterator j;
i = registeredClientInfo.equal_range(name);
if (j == registeredClientInfo.end())
return;
for(j=i.first;j != i.second;++j)
{
if((j->second) == sessionid) registeredClientInfo.erase(j->second);
}
for(j=i.first;j != i.second;++j)
{
cout<<""<<j->second<<endl;///This prints the erased values too;
}
}
Am i doing something wrong?
Any help in this regard greatly appreciated.
Most important, you call
erase(j->second), when you meant to callerase(j). You’re not erasing the element of the multimap pointed to byj, you’re erasing all elements whose keys are equal to the value of the element pointed to byj(which issessionid). I expect that’s nothing.Also: call
equal_rangeagain after theeraseloop is complete – the effect of using an erased iterator is undefined, so if you erased the first iteratori.first, then you can’t start iterating from there again afterwards.Note that this also means there’s a bug in your loop that does the
erase, since in the case that you do callerase, you incrementjwhen it holds an iterator value that’s no longer valid. Unfortunately, the correct code is:Or if you prefer:
Or if the entry is unique for the key/value pair, so that you only have to remove at most one thing, then:
if (j == registeredClientInfo.end()) return;isn’t right either, sincejis uninitialized when you do it. If the key isn’t found, thenequal_rangereturns an empty range (two equal iterator values), so your other loops will do nothing anyway.