I want to move a range of element in a list to the front of an element also in the same list. I'm trying to use
list.splice(const_iterator pos, list &other, const_iterator first, const_iterator last)
to implement this.
This is my code:
list<int> ls{1, 2, 3, 4, 5, 6, 7, 8, 9}; // try to move [6, 9] to the front of 5 auto pos = find(ls.cbegin(), ls.cend(), 5), first = ++pos; ls.splice(pos, ls, first, ls.end());
cppreference says:
Transfers the elements in the range [first, last) from other into *this. The elements are inserted before the element pointed to by pos. The behavior is undefined if pos is an iterator in the range [first,last).
Actually pos != first, but this causes a infinite loop. What causes this to happen, and how to solve it?
https://stackoverflow.com/questions/67362922/how-to-move-a-range-of-element-in-a-list-to-the-front-of-an-element-also-in-the May 03, 2021 at 10:44AM
没有评论:
发表评论