Hey,
Is it possible to swap elements in a list? For example, if my list looked like
Is there some way to switch the 3 and 4 around, how it would then look like {1, 2, 4, 3} ?Code:list<int> myList = {1, 2, 3, 4} (incorrect syntax, i know)
Hey,
Is it possible to swap elements in a list? For example, if my list looked like
Is there some way to switch the 3 and 4 around, how it would then look like {1, 2, 4, 3} ?Code:list<int> myList = {1, 2, 3, 4} (incorrect syntax, i know)
"I don't fail - I succeed at finding things that don't work"
Website Promotion Techniques @AbstractPromotion.com
Did you try the std::swap() function in <algorithm>?
I think you should be able to just swap the iterators to 2 elements.
But how would you randomly access elements in list? And since you can't randomly access list elements, you'd have to swap iterators, or iter_swap, which you'd have increment/decrement through a tedious process to get where you intend it to be.
What are you wanting to swap item positions for... Are you implementing a sorting algorithm?
Are the ones you want to swap always next to each other?
btw You aren't forced to be bound by the notion of swapping when it comes to lists, and indeed it can be better to not attempt to do such things. For example you can achieve the reordering you want by removing 4, and reinserting it between 2 and 3.
Whatever you do, don't treat a list as though it provides random access.
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"