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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:34:57+00:00 2026-05-29T11:34:57+00:00

I experimented the following code list<int> a ={1,2}; list<int> b ={3,4}; a.erase(b.begin()); for (auto

  • 0

I experimented the following code

list<int> a ={1,2};
list<int> b ={3,4};
a.erase(b.begin());
for (auto x:b) cout << x << endl;

Strangely, the program ran fine without any error. What it prints out is 4. I wonder why erase is a member function when the object is already implicit in the iterator.

  • 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-29T11:34:57+00:00Added an answer on May 29, 2026 at 11:34 am
    a.erase(b.begin());
    

    This invokes undefined behavior, because you’re passing iterator obtained from one container to a function of other container. a and b are two different containers, they’re not same.

    Undefined behavior means anything could happen: it may run as expected, or it may not. Neither the language specification nor the compiler gives guarantee that it will work. It is aptly said “undefined behavior”.

    What you should do is this:

    auto value = *(b.begin()); //value is int
    auto it = std::find(a.begin(), a.end(), value); //returns iterator 
    if ( it != a.end())
       a.erase(it); //well-defined, as the iterator belongs to the same container!
    

    Or, if you want to remove all elements equal to value, then you could simply do this:

    a.remove(value); //std::list has remove member function
    

    However, if you use std::vector which you should be using in most cases. It is default container type in C++, and you should use std::list only if you’ve strong reason to do so:

    std::vector<int> a ={1,2};
    std::vector<int> b ={3,4};
    
    //if you want to remove one element:
    auto value = *(b.begin()); //value is int
    auto it = std::find(a.begin(), a.end(), value); //returns iterator 
    if ( it != a.end())
       a.erase(it); //well-defined, as the iterator belongs to the same container!
    

    And if you want to remove all elements equal to value, then you can apply popular Erase-Remove Idiom as:

    a.erase(std::remove(a.begin(), a.end(), value), a.end()); 
    

    Note that std::vector doesn’t have remove() member function, that is why you apply this idiom. You can read my answer here which discusses about this in more detail.

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

Sidebar

Related Questions

As an experiment I ran the following code snippet (simplified): public class SGamePlay extends
The following code require 'timeout' begin timeout(20) do # Line 4 result = `hostname`
I have the following code: public static class ScraperMasterUriDetails { public static Dictionary<Guid, string>
The following code makes me mad: private String blahBlah(){ return null; } @Test public
What is the pattern and/or how would you change the following code so that
I made the following code example to learn how to use a generics method
I just experimented with the addLocalMonitorForEventsMatchingMask:handler: method in NSEvent and came across the following
My setup: Rails 2.3.10, Ruby 1.8.7 I have experimented, without success, with trying to
I placed the following code in two pages. A simple aspx page, there it
The following code (which compiles and executes properly, doing what I want) is a

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.