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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:59:52+00:00 2026-06-04T04:59:52+00:00

I have std::list eg. with ints: 9 10 8 25 70 75 30 14

  • 0

I have std::list eg. with ints: 9 10 8 25 70 75 30 14 80

I want to move all elements less than 10 after the some_position_number element eg. = 5. The initial order of moved objects is important and must be the same as initial after moving.

In other words at the end need to receive some_position_number elements that false and after them true

Like it must be for 1st example: 10 25 70 75 30 9 *8* 14 80

Second initial: 9 3 8 25 70 75 30 14 80
Second result: 10 25 70 75 30 9 *3* 8 14 80

Third initial: 25 70 75 30 14 9 3 8 80
Third result: 25 70 75 30 14 9 3 8 80 (the same already 5 at the beginning)

4 initial: 3 4 1 2 3 9 3 8 80
4 result: 9 3 8 80 3 4 1 2 3 (something like this) seems here some_position_number must be used as threshold or 80 3 4 1 2 3 9 3 8 also accepted, but seems need to check for end() and for infinity loop?

How to do this most effective way, maybe without additional list to avoid unnecessary objects creation and erasing? because at the real app there is no ints at the std::list, but objects. Maybe std::splice? Somehow select objects that need to move, than find new position and std::splice every element to it.

  • 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-04T04:59:53+00:00Added an answer on June 4, 2026 at 4:59 am

    It’s ugly, but it works and I believe it properly handles all iterator invalidation situation correctly:

    #include <iostream>
    #include <list>
    
    using namespace std;
    
    list<int> l;
    
    // prototypes for various helpers of no consequence to the question
    void  init();
    bool my_pred( list<int>::value_type val);
    void dump_list( list<int> const& l);
    
    
    typedef list<int>::iterator iter_t;
    
    int main() 
    {
        init();
        dump_list(l);
    
        // we want to remove elements that meet the predicate until
        // there are `some_position_number` elements that didn't meet 
        // the predicate
    
        int some_position_number = 5;
        list<int> tmp_list;
    
        iter_t i = l.begin();
        for (int count = 0; count < some_position_number && i != l.end(); ) {
            iter_t tmp(i);
            ++i;
            if (my_pred(*tmp)) {
                tmp_list.splice( tmp_list.end(), l, tmp); // remove an element
            }
            else {
                ++count;
            }
        }
    
        // now i points at the position we want to insert the elements we removed:
    
        l.splice( i, tmp_list);
    
        dump_list(l);
    }
    
    
    
    
    bool my_pred( list<int>::value_type val)
    {
        return val < 10;
    }
    
    void  init()
    {
        l.push_back(9);
        l.push_back(10);
        l.push_back(8);
        l.push_back(25);
        l.push_back(70);
        l.push_back(75);
        l.push_back(30);
        l.push_back(14);
        l.push_back(80);
    }
    
    void dump_list( list<int> const& l)
    {
        for (list<int>::const_iterator i = l.begin(); i != l.end(); ++i) {
            cout << *i << " ";
        }
    
        cout << endl;
    }
    

    The keys are:

    • the iterator i that’s used to walk the range of elements that might be moved has to be copied then incremented before calling splice(). When splice() is called, the original i (now tmp) is invalidated.
    • we splice the elements that we want to move on to a temporary list. While this requires a second list, it still doesn’t involve a copy of the elements – we’re just moving them somewhere else to keep bookkeeping easy.
    • keep a count of the elements that didn’t meet the predicate – this lets use figure out the insertion point as we move along checking for elements to move.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

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 already the list pointer of CDrawObject* std::list<CDrawObject*> elements; How I can move
I have a std::list of boost::shared_ptr<T> and I want to remove an item from
I have a std::vector<int> , and I want to delete the n th element.
Possible Duplicate: Does std::list::remove method call destructor of each removed element? I have a
I have a std::list of Bananas , and I want to get rid of
I have one std::list<> container and these threads: One writer thread which adds elements
I have a linked list that I want to sort part of, eg: std::sort(someIterator,
Possible Duplicate: Does std::list::remove method call destructor of each removed element? I have a
Is it possible to remove an element from an std::list if you have only

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.