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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:06:45+00:00 2026-05-26T21:06:45+00:00

I have some thing like this code: map<int, string> m; m[1] = a; m[2]

  • 0

I have some thing like this code:

map<int, string> m;

m[1] = "a";
m[2] = "b";
m[3] = "a";
m[4] = "a";
m[5] = "e";
m[6] = "f";
m[7] = "g";
m[8] = "h";
m[9] = "i";

for (it1 = src.begin(); it1 != src.end(); ++it1) {

    for (it2 = it1; it2 != src.end(); ++it2) {
        if (it2 == it1) {
            continue;
        }

        if (it2->second == it1->second) {
            fprintf(stderr, "%u\n", it2->first);
            src.erase(it2);
        }
    }
}

I use map, because the elements is not always in this order (1, 2 …)
So here is the question

In some cases of map values, this code print this

2
3
4
6
7
8
9
5

How it is possible (skipping 5), if map sort by container in order 1, 2 … and so on ?

  • 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-26T21:06:45+00:00Added an answer on May 26, 2026 at 9:06 pm

    Your erase loop is off. The typical idiom is this:

    for(std::map<K,V>::const_iterator it = v.begin(); it != v.end() /* not hoisted! */; /* no increment */ )
    {
      // do something
      if (suitable_condition)
      {
        v.erase(it++);
      }
      else
      {
        ++it;
      }
    }
    

    Your code erroneously performs an increment on an invalid iterator (namely it2 after the erase), which is undefined behaviour.

    (For certain other container types, erase returns an iterator to the next valid element, so in those cases you would say it = v.erase(it); but the details of the best erasing pattern depend on the concrete container type.)

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

Sidebar

Related Questions

I have a code like this Map<String, List> searchMap = new HashMap<String, List>(); for(int
Let's say I have some code like this if(isset($_GET['foo'])) //do something if(isset($_GET['bar'])) //do something
In our code we used to have something like this: *(controller->bigstruct) = ( struct
In a bunch o' places in my code, I have something like this: public
In my code, I have something that looks like this: @implementation MyClass - (id)
I have jQuery code which looks something like this on Button1 Click $('table.result_grid tbody
I have code which needs to do something like this There is a list
Right now, I have code that looks something like this: Private Sub ShowReport(ByVal reportName
I have a program that simulates mouse click. Code is something like this: [DllImport(user32.dll,
When you have code like this: Something something = new Something(); BlahEntities b =

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.