Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6886267
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:44:16+00:00 2026-05-27T05:44:16+00:00

My requirement is to delete a a value from the multimap and not the

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T05:44:17+00:00Added an answer on May 27, 2026 at 5:44 am

    Most important, you call erase(j->second), when you meant to call erase(j). You’re not erasing the element of the multimap pointed to by j, you’re erasing all elements whose keys are equal to the value of the element pointed to by j (which is sessionid). I expect that’s nothing.

    Also: call equal_range again after the erase loop is complete – the effect of using an erased iterator is undefined, so if you erased the first iterator i.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 call erase, you increment j when it holds an iterator value that’s no longer valid. Unfortunately, the correct code is:

    for(j=i.first;j != i.second;)
    {
        if((j->second) == sessionid) {
            auto next = j;
            ++next;
            registeredClientInfo.erase(j);
            j = next;
        } else {
            ++j;
        }
    }
    

    Or if you prefer:

    for(j=i.first;j != i.second;)
    {
        auto current = j;
        ++j;
        if((current->second) == sessionid) registeredClientInfo.erase(current);
    }
    

    Or if the entry is unique for the key/value pair, so that you only have to remove at most one thing, then:

    for(j=i.first;j != i.second;++j)
    {
        if((j->second) == sessionid) {
            registeredClientInfo.erase(j);
            break;
        }
    }
    

    if (j == registeredClientInfo.end()) return; isn’t right either, since j is uninitialized when you do it. If the key isn’t found, then equal_range returns an empty range (two equal iterator values), so your other loops will do nothing anyway.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a requirement for reading, updating and deleting a file. I want to
My requirement is just to display a set of values retrieved from database on
I have a requirement where i have two records with same value except the
what is the best way to delete multiple records using hibernate.My requirement is i
My requirement like this: I have to fetch a row from table A. Now
I had a small requirement of making a specific attribute as primary key, such
Hi all I am having a requirement like selecting multiple values and then to
Hi i all having a requirement to delete particular data from a text file.
We are using PostgreSQL. My requirement is to delete unused sequences from my database.
I have a requirement to delete all files in a directory, except for the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.