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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:29:43+00:00 2026-05-25T12:29:43+00:00

This code is written on fly, plz ignore syntax mistakes if any. std::list<MY_STRUCT> myList;

  • 0

This code is written on fly, plz ignore syntax mistakes if any.

std::list<MY_STRUCT> myList;

MY_STRUCT theStruct;

myList.push_back( theStruct );
myList.push_back( theStruct );

// assume I store the pointer of the last item (the 2nd item in this case).
MY_STRUCT * item2 = &myList.back();

// I added another item
myList.push_back( theStruct );

// now I want to delete item2 that I stored bases on its pointer.
// Can myList.remove_if(...) help if so how?

I want to delete the middle item in the list by its pointer (assume I have the pointer value).

I know I can iterate through the list and look for this pointer but is there a better way? Does STL provide a function to do it..Can I use remove_if() in this case to delete the item?

  • 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-25T12:29:43+00:00Added an answer on May 25, 2026 at 12:29 pm

    Sure, list::remove_if uses whatever condition you give it. For example

    template <typename T>
    struct AddressIs {
        T *ptr;
        AddressIs(T *ptr) : ptr(ptr) {}
        bool operator()(const T &object) const {
            return ptr == &object;
        }
    };
    
    myList.remove_if(AddressIs<MY_STRUCT>(item2));
    

    Mankarse’s point is good though – if you can use an iterator instead of a pointer to identify the item you’re interested in, then you don’t need to mess about with this.

    Beware also that we’re relying here on the fact that the address of an item in a list stays the same forever. That isn’t always true of all collections, for example vector might have to relocate all the data when you call push_back. If it does, then your middle item is no longer pointed to by item2. Each collection documents which operations can invalidate iterators and/or references to elements.

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

Sidebar

Related Questions

I have this code written in .NET 4.0 using VS2010 Ultimate Beta 2: smtpClient.Send(mailMsg);
I've written this code to try and send a url as a post value
I have written this code to join ArrayList elements: Can it be optimized more?
I have written this code to convert string in such format 0(532) 222 22
I've sometimes seen code written like this : public class B1 { } public
I have this code in my ASP.NET application written in C# that is trying
i'm wondering about this possibility. Is it possible to make our code written in
In our application, I have seen code written like this: User.java (User entity) public
I've written this sample code to print the length of x. But for some
I've written this piece of code where I've assigned an unsigned integer to two

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.