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 8420431
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T02:53:10+00:00 2026-06-10T02:53:10+00:00

I have a multimap with Note objects from which I want to remove only

  • 0

I have a multimap with Note objects from which I want to remove only one object. There can be several Note objects with the same key. The problem is that right now there’s also objects being removed that aren’t within the key range I specify:

long key = note.measureNumber * 1000000 + note.startTime; // = 2000001
multimap<long, Note>::iterator it;
for (it = noteList.lower_bound(key); it != noteList.end() && it->first < (key + 1); it++) {
    if(it->second.frequency == note.frequency){
        noteList.erase(it);
    }
}

When I run this code with key 2000001 the object I’m able to erase the right object, but another object with key 1000017 gets erased as well. Both objects have the same frequency though.

Any idea’s what is wrong with my for loop?

EDIT: To be clear, I only want to check for objects with one specific key (in this case 2000001) there’s no need for the iterator to look at objects with different keys than that one.

  • 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-06-10T02:53:10+00:00Added an answer on June 10, 2026 at 2:53 am

    Once you erase an iterator it becomes invalid. If you wish to erase from a map while you iterate through it, your code needs to change. Try this:

    multimap<long, Note>::iterator it;
    for (it = noteList.lower_bound(key); it != noteList.end() && it->first < (key + 1);) {
        if(it->second.frequency == note.frequency){
            noteList.erase(it++);
        }
        else
        {
            ++it;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a multimap from which I want to free the memory allocated to
Have deployed numerous report parts which reference the same view however one of them
Is there a simple or standard way to have a multimap iterator which iterate
I'm debugging my code and at one point I have a multimap which contains
I have a std::multimap , and I want to create a boost::iterator_range from equal_range
have written this little class, which generates a UUID every time an object of
Suppose that I have a multimap (which maps multiple values to a single key),
I have std::list<multimap<std::string,std::string>::iterator> > Now i have new element: multimap<std::string,std::string>::value_type aNewMmapValue(foo1,test) I want to
I have 2 different object types stored in RavenDb, which are a parent/child type
I have a Multimap<Date,Invoice> multimap = ArrayListMultimap.create(); from guava. I was wondering how to

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.