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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T00:54:20+00:00 2026-06-07T00:54:20+00:00

Rehrasing the question – Question rephrased – I have a requirement where I need

  • 0

Rehrasing the question –
Question rephrased –
I have a requirement where I need to replace a pair with a new key and value. Consider this –

#include <map>
#include <string>
#include <iostream>

using namespace std;

int main()
{
    std::multimap<unsigned int, std::string> mymap;

    mymap.insert(std::multimap<unsigned int, std::string>::value_type(0, "A1"));
    mymap.insert(std::multimap<unsigned int, std::string>::value_type(0, "A2"));
    mymap.insert(std::multimap<unsigned int, std::string>::value_type(2, "C1"));
    mymap.insert(std::multimap<unsigned int, std::string>::value_type(2, "C2"));
    mymap.insert(std::multimap<unsigned int, std::string>::value_type(1, "B1"));
    mymap.insert(std::multimap<unsigned int, std::string>::value_type(1, "B2"));
    mymap.insert(std::multimap<unsigned int, std::string>::value_type(1, "B3"));

    std::pair<std::multimap<unsigned int, std::string>::iterator, std::multimap<unsigned int, std::string>::iterator> pr = mymap.equal_range(1);

    std::multimap<unsigned int, std::string>::iterator it;
    for (it=pr.first; it!=pr.second; ++it)
    {
        unsigned int key = it->first;
    key = key+10;

        std::string val = it->second;
        val = "X" + val;
        mymap.erase(it);
        mymap.insert(std::multimap<unsigned int, std::string>::value_type(key, val));

    }

    for ( it=mymap.begin() ; it != mymap.end(); it++ )
    {
        cout << (*it).first << " => " << (*it).second << endl;
    }

    return 0;

}

The program crashes on visual studio 2008 since the iterator is invalidated.

I expect it to be:

0 => A1
0 => A2
2 => C1
2 => C2
11 => XB1
11 => XB2
11 => XB3

The idea is I want to replace an existing entry in the map with a new entry.

What am I doing wrong? Any help very much 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-06-07T00:54:23+00:00Added an answer on June 7, 2026 at 12:54 am

    The trick is to advance the iterator first, then erase a copy of the iterator.

    std::multimap<unsigned int, std::string>::iterator it = pr.first;
    while (it != pr.second)
    {
        unsigned int key = it->first;
        key = key+10;
    
        std::string val = it->second;
        val = "X" + val;
    
        std::multimap<unsigned int, std::string>::iterator itCopy = it;
        ++it;
        mymap.erase(itCopy);
        mymap.insert(std::multimap<unsigned int, std::string>::value_type(key, val));
    }
    

    In C++11, you can do this:

    std::multimap<unsigned int, std::string>::iterator it = pr.first;
    while (it != pr.second)
    {
        unsigned int key = it->first;
        key = key+10;
    
        std::string val = it->second;
        val = "X" + val;
    
        it = mymap.erase(it);
        mymap.insert(std::multimap<unsigned int, std::string>::value_type(key, val));
    }
    

    By the way, because this code is increasing the key each time, it will process every element over and over again.

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

Sidebar

Related Questions

I have asked this question before but I haven't got an answer, so I'm
This is a pretty open ended question. I have a T-Mobile Touch and have
This question is no longer relevant now that microsoft have released a proper version
I'm reading this question because I have to load a ton of downloaded resources
I'm rephrasing this question because it was either too uninteresting or too incomprehensible. :)
I've asked this question already this week but am rephrasing it with the hope
This question came today in the manipulatr mailing list. http://groups.google.com/group/manipulatr/browse_thread/thread/fbab76945f7cba3f I am rephrasing. Given
as Microsoft are releasing a tablet later this year, with a new operating system
some comments here have confused me! I thought I knew this and god knows
The code for this question is too long to be of any use. But

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.