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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T01:47:52+00:00 2026-06-07T01:47:52+00:00

I have a std::list<obj*> , where obj is my class: std::list<obj*> list_of_ptr; list_of_ptr.push_back(new obj());

  • 0

I have a std::list<obj*>, where obj is my class:

std::list<obj*> list_of_ptr;
list_of_ptr.push_back(new obj());

I want to convert this list to the equivalent std::list<obj>, after that I no longer need the list_of_ptr.

What is the fastest way to do this work?

  • 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-07T01:47:55+00:00Added an answer on June 7, 2026 at 1:47 am

    std::transform is your friend:

    std::vector<obj> objects;
    std::transform(
        list_of_ptr.begin(), list_of_ptr.end(),
        std::back_inserter(objects), 
        [](obj* p) { return *p; });
    

    Or, if C++11 lambda expressions cannot be used, one may use a simple function object to perform the indirection:

    struct indirect
    {
        template <typename T>
        T& operator()(T* p) { return *p; }
    };
    
    std::transform(
        list_of_ptr.begin(), list_of_ptr.end(),
        std::back_inserter(objects), 
        indirect());
    

    Or, using boost::indirect_iterator:

    std::vector<obj> objects(
        boost::make_indirect_iterator(list_of_ptr.begin()),
        boost::make_indirect_iterator(list_of_ptr.end()));
    

    These, of course, assume that there are no null pointers in the sequence. It is left as an exercise for the reader to figure out how to correctly manage the lifetimes of the objects pointed to by the pointers in list_of_ptr.

    Ideally, one would use a std::vector<obj> from the start, or, if that is not possible, a container of smart pointers. Manually managing the lifetimes of the pointed-to objects, and doing so correctly, is very difficult. C++ has awesome automatic object lifetime management facilities (destructors, smart pointers, containers, stack semantics, RAII), and there is no reason not to use them.

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

Sidebar

Related Questions

I have a class that looks like this: typedef std::list<char*> PtrList; class Foo {
I have a class that includes a std::list and wish to provide public begin()
I have a custom iterator template class that wraps up a std::list iterator. In
I have std::list<multimap<std::string,std::string>::iterator> > Now i have new element: multimap<std::string,std::string>::value_type aNewMmapValue(foo1,test) I want to
I have a linked list that I want to sort part of, eg: std::sort(someIterator,
I have a std::list of boost::shared_ptr<T> and I want to remove an item from
Say you have a std::list of some class. There are two ways you can
I have class A with a public std::list<int> object list_ . Class B ,
I have a std::list of Bananas , and I want to get rid of
Say I have a std::list of class T 's: std::list<T> l; When passing it

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.