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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:00:20+00:00 2026-06-13T05:00:20+00:00

I am using STL list for my linked list implementation but when I am

  • 0

I am using STL list for my linked list implementation but when I am using erase function inside a loop it is giving segmentation fault. Can someone tell me why is it happening?

void remove(list<int> &myList,int N){
    int k = 1;
    list<int>::iterator it;
    for(it = myList.begin(); it != myList.end();it++){
        if(k == N){
            myList.erase(it);
            k = 1;
        }
        else
            k++;
    }
}
  • 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-13T05:00:21+00:00Added an answer on June 13, 2026 at 5:00 am

    When you call erase on an iterator, it invalidates that iterator. But you continue to use it. You need to capture the return value of erase, and assign that back to your iterator, like this:

    it = myList.erase(it);
    

    But this will necessitate a slight change in your loop. If you erase, then you don’t want to increment, because then you will be skipping one element. This is especially bad if you end up erasing the last element, because then you will be moving past the end iterator. So, you should only increment if you don’t erase:

    for(it = myList.begin(); it != myList.end(); ){
        if(k == N){
            it = myList.erase(it);
            k = 1;
        }
        else
        {
            k++;
            ++it;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Sort list using stl sort function My question is can we sort
Possible Duplicate: Sort list using stl sort function The C++ standard library gives strict
Ok this time I decided to make a list using the STL. I need
I am not using boost libraries. How can i do this using STL? class
I've got a program that populates a STL linked list with structs, and I'm
I'm using an STL std::multiset<> as a sorted list of pointers. The sort order
Note: I accidentally posted this question without specifying which STL implementation I was using,
I'm implementing a hash table and linked list in C++ (no STL - don't
Let's say I create a linked list in STL: list<int, my_allocator<int> > data; Then
I'm working on a problem which requires me to use the STL linked list

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.