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

  • Home
  • SEARCH
  • 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 9036679
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T09:03:47+00:00 2026-06-16T09:03:47+00:00

I’ve tried removing the elemetns (pointers to a named type) from a list using

  • 0

I’ve tried removing the elemetns (pointers to a named type) from a list using std::remove (I’ve read about this algorithm here and here) and a combination of list::erase and std::find.

Here’s the code that I wrote for this purpose:

#include <iostream>

#include <list>
#include <functional>
#include <string>
#include <algorithm>


class NamedType
{
    std::string name_; 

    public: 
        NamedType (const char* name)
            : 
                name_(name)
        {}    

        void info()
        {
            std::cout << name_ << ": NamedType::update()" << std::endl;
        }
};

class NamedTypeList
{
    std::list<NamedType*> objectList_; 

    public: 

        void addNamedType(NamedType& o)
        {
            NamedType* oPtr = &o; 
            objectList_.push_back(oPtr);
        }

        void removeNamedTypeWithFind(NamedType& o)
        {
            std::list<NamedType*>::iterator removedNamedType = std::find(
                objectList_.begin(), objectList_.end(), &o);

            if (removedNamedType != objectList_.end())
            {
                objectList_.erase(removedNamedType); 
            }
        }

        void removeNamedType(NamedType& o)
        {
            std::remove(objectList_.begin(), objectList_.end(), &o); 
        }

        void namedObjectsInfo()
        {
            std::for_each(objectList_.begin(), objectList_.end(), 
                std::mem_fun(&NamedType::info)); 
        }
};

using namespace std;

int main ()
{
    NamedType o1("o1"); 
    NamedType o2("o2"); 
    NamedType o3("o3"); 
    NamedType o4("o4"); 

    NamedTypeList objectList1; 
    NamedTypeList objectList2; 

    objectList1.addNamedType(o1); 
    objectList1.addNamedType(o2); 
    objectList1.addNamedType(o3); 
    objectList1.addNamedType(o4); 

    objectList2.addNamedType(o1); 
    objectList2.addNamedType(o2); 
    objectList2.addNamedType(o3); 
    objectList2.addNamedType(o4); 

    cout << "Registered objects into objectList1:" << endl;
    objectList1.namedObjectsInfo(); 

    cout << "Registered objects into objectList2:" << endl;
    objectList2.namedObjectsInfo(); 

    cout << "Removing o2 object from objectList1 with remove" << endl;

    objectList1.removeNamedType(o2); 
    objectList1.namedObjectsInfo(); 


    cout << "Removing o2 object from objectList2 with std::find" << endl;

    objectList2.removeNamedTypeWithFind(o2); 
    objectList2.namedObjectsInfo(); 

}; 

What I don’t get is why I get the following output when I call objectList1.removeNamedType(o2); :

Removing o2 object from objectList1 with remove
o1: NamedType::update()
o3: NamedType::update()
o4: NamedType::update()
o4: NamedType::update()

I’m having trouble understanding the documentation: I get that there is a new_end iterator showing the new end of the range, but this does not work then if I have multiple same NamedTypes. E.g. if I register the object o2 twice in the objectList1, it will be visible and its member function will be called by the namedObjectsInfo() method since it loops over all elements (it doesn’t see the new_end iterator).

If I understood it right, should I even use std::remove to remove elements from a container, or a combination of std::find and list::erase in this case?

  • 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-16T09:03:48+00:00Added an answer on June 16, 2026 at 9:03 am

    When a container has its own remove method, as std::list does, you should use that instead of std::remove. For containers that do not have a remove method, you should use the erase-remove idiom described in other answers here.

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

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I am using jsonparser to parse data and images obtained from json response. When
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.