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.
It’s ugly, but it works and I believe it properly handles all iterator invalidation situation correctly:
The keys are:
ithat’s used to walk the range of elements that might be moved has to be copied then incremented before callingsplice(). Whensplice()is called, the originali(nowtmp) is invalidated.