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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:40:06+00:00 2026-05-20T11:40:06+00:00

My STL is a bit rusty, so forgive me for asking a possibly trivial

  • 0

My STL is a bit rusty, so forgive me for asking a possibly trivial question. Consider the following piece of code:

map<int,int> m;
...
for (auto itr = m.begin(); itr != m.end(); ++itr) {
    if (itr->second == 0) {
        m.erase(itr);
    }
}

The question is: Is it safe to erase elements while looping over the map?

  • 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-20T11:40:06+00:00Added an answer on May 20, 2026 at 11:40 am

    I think that you shouldn’t use removed iterator at all – in case of lists this causes serious problems, shouldn’t be different for maps.

    EDIT by Matthieu M: this code is well-formed in C++0x and allowed as an extension by MSVC.

    map<int,int> m;
    ...
    auto itr = m.begin();
    while (itr != m.end())
    {
        if (itr->second == 0) {
            itr = m.erase(itr);
        }
        else 
        {
            itr++;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've always been a bit confused about how STL containers (vector, list, map...) store
I'm a bit confused about using STL set::find() for a set of my own
In the STL almost all containers have an erase function. The question I have
Typically you will find STL code like this: for (SomeClass::SomeContainer::iterator Iter = m_SomeMemberContainerVar.begin(); Iter
I'm having a bit of an odd problem while trying to compile some code
I've always wanted a bit more functionality in STL's string. Since subclassing STL types
I need some variable/struct to store 32hex number or 128-bit number in an STL
I am having a bit of trouble deep copying a pointer to a map
I'm having some difficulties while parsing a binary STL file with Python (2.7.1 32-bit
I'm writing my own implementation of the C++ STL map container. Now I'm trying

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.