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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:26:08+00:00 2026-06-15T15:26:08+00:00

How do you manage a situation where objects inside a std::list need to move

  • 0

How do you manage a situation where objects inside a std::list need to move their position in, or even remove themselves from that list, while you are iterating over that list?

A complicated example follows.

You have an ordered list of objects.

list< Object* > objects ;

You have a loop that iterates over each

for( list< Object* >::iterator iter = objects.begin() ; iter != objects.end() ; ++iter )
{
    // call "move()" on each
    (*iter)->move() ;
}

Now here’s the catch. Object::move() sometimes reorders the list, the very list that we are iterating on with the loop above.

I’m not sure how to engineer this. How can I write and invoke a member function Object::move() that possibly re-orders the list every time you call it, and iterate over each element in the objects list, invoking Object::move() on each Object in the list?

  • 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-15T15:26:10+00:00Added an answer on June 15, 2026 at 3:26 pm

    Holistically.

    Putting a member in the contained object that knows about and manipulates the container is almost universally a bad design choice. You want container->contained but not contained->container.

    Your sorting routine is going to have to be just that, a sorting routine that uses whatever rules “move” would otherwise use to decide when to swap things around. You are basically ordering your list by some condition when you call “move” over all its elements, so you just change this idea of elements moving themselves to them being moved by something that knows how they need to be ordered. When you do this of course that condition can be different for different client needs.

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

Sidebar

Related Questions

my situation is this: I have similar objects that differ in certain fields. Example:
Situation I have a few single-responsibility Servlets that take a request, do their job,
I have a situation where I'm creating a number of IDisposable objects that encapsulate
I have a mutable array of managed objects that I get from Core Data
I have a situation in a new WinRT app where I need to manage
I manage to identify duplicate records from two different databases: select * from taskperformance
I manage a website for an organization that has a network where several hundred
thank you for reading my question. Following situation: -A sharepoint list, based on a
I just wondered how people were approaching this situation. It's something that seems like
I'm attempting to use the ReaderWriterLockSlim class to manage a list. There are many

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.