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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T14:03:06+00:00 2026-06-08T14:03:06+00:00

New to cpp. When I call to erase on iterator without increment it my

  • 0

New to cpp.
When I call to erase on iterator without increment it my program exit immediately without any sign or crash. In the following example only ‘After if condition ‘ is printed and than nothing. no error displayed. however if i do the correct call to list.erase(it++); than it print all.
my question is about what happened when I don’t call it correct. why i don’t see any crash or exit?
My worried and the reason that i asked the question is about catching these kind of crashes, why i didn’t catch it in the catch block? is there a way to catch it?

int main(int argc, char *argv[]) {
    try {
        list<int>::iterator it;
        list<int> list;
        list.push_back(1);
        for (it = list.begin(); it != list.end(); ++it) {
            if ((*it) == 1) {
                list.erase(it);
            }
            cerr << "After if condition " << endl;
        }
        cerr << "After for condition" << endl;
    } catch (...) {
        cerr << "catch exception" << endl;
    }
    cerr << "Finish" << endl;
}
  • 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-08T14:03:07+00:00Added an answer on June 8, 2026 at 2:03 pm

    You may not alter the container when using iterators. erase invalidates the iterator immediatley.

    What you can do is something like:

    for( list<int>::iterator it = list.begin(); it != list.end();)
      if( (*it) == 1 )        
        it=list.erase(it);
      else
        ++it;
    

    (from STL list erase items)

    or

    for( list<int>::iterator it = list.begin(); it != list.end();)
      if( (*it) == 1 )        
        list.erase(it++);
      else
        ++it;
    

    There are many posts on that. Search for “erase list iterator”!

    edit: If you do otherwise, i.e. invalidate the iterator and increasi afterwards, the behaviour is undefined. That means it can differ from system to system, compiler to compiler, run to run.

    If it doesn’t crash, and gives proper behaviour, you’re lucky. If it doesn’t crash and does something else, you’re unlucky, because it’s a hard-to-find bug.

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

Sidebar

Related Questions

I am new to NDK. I have a cpp file which has the following
I created a new HalloWorld Makefile Project. There is a HalloWorld.cpp with my main
New to Regex. I want to validate to this format: Any character allowed, except
New to spring. I'm getting the following exception being caught: 2012-06-14 16:20:57,719 [http-8080-6] ERROR
The following snippet gives the warning: [C++ Warning] foo.cpp(70): W8030 Temporary used for parameter
I have a main function in A.cpp which has the following relevant two lines
I am trying to call another function, foo(), in the Raw class from main.cpp
I am new to programming. I am porting cpp (WIN32) to cocoa framework. I
I'm new to C++ and need some help with namespaces. Following are my 4
I am new to Objective C. I am porting the cpp code to Objective

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.