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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T20:32:34+00:00 2026-06-05T20:32:34+00:00

I have an STL list of pointers, along with another pointer of the same

  • 0

I have an STL list of pointers, along with another pointer of the same type. I need to perform a ton of operations on each of them. My current method is to push the pointer onto the list, iterate through everything, then pop the pointer back off. This works fine, but it got me wondering if there were a more elegant/less hacky way to iterate through a combination of things. (say if I had a pile of other additional things to add to the iteration)

The current functional, but a bit hacky way:

std::list<myStruct*> myList;
myStruct* otherObject;

//the list is populated and the object assigned

myList.push_back(otherObject);
for(std::list<myStruct*>::iterator iter = myList.begin(); iter != myList.end(); ++iter){

      //Long list of operations

}

myList.pop_back(otherObject);
  • 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-05T20:32:35+00:00Added an answer on June 5, 2026 at 8:32 pm

    The more idiomatic approach might be to encapsulate your “long list of operations” into a function, then call it as needed. For example:

    void foo (myStruct* x)
    {
        // Perform long list of operations on x.
    }
    
    ...
    
    {
        std::list<myStruct*> myList;
        myStruct* otherObject;
    
        // The list is populated and the object assigned.
    
        foo (otherObject);
        for(std::list<myStruct*>::iterator iter = myList.begin(); iter != myList.end(); ++iter)
        {
            foo(*iter);
        }
    }
    

    Then, if you later need to apply foo to other items, just call as needed.

    While there’s nothing inherently evil about adding otherObject to myList in the manner you describe, it is in a way abusing the list and should probably be avoided if possible.

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

Sidebar

Related Questions

I have a stl::list containing Widget class objects. They need to be sorted according
I want to have a stl list of objects where each object contains two
I have a thread push-backing to STL list and another thread pop-fronting from the
I have a chess game that originally used an stl list to store the
Suppose I have a STL map where the values are pointers, and I want
I have a class TContainer that is an aggregate of several stl collections pointers
I have already the list pointer of CDrawObject* std::list<CDrawObject*> elements; How I can move
I have a problem with the STL list class . I have a base
I'm having a problem using inheritance and the STL list library... Say, I have
I have a list of pointers to classes: list<AbsClient*> clients; AbsClient could be 1

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.