Move list element to the end in STL

送分小仙女□ 提交于 2019-12-28 16:30:21

问题


I have already the list pointer of CDrawObject*

std::list<CDrawObject*> elements;

How I can move some element to the end of list. I see STL Algorithms Reference but i don't find this operations. How i can do it?


回答1:


Use the list method splice()

void list::splice ( iterator position, list<T,Allocator>& x, iterator i );

Move iterator i from list x into current list at position "position"

Thus to move it to the end put

x.splice( x.end(), x, iter );

(they can both be the same list or different lists as long as the list from which the item is moved has the same type, both T and Allocator)




回答2:


A std::list is a doubly-linked list, which means you do not have random access to element n. You have to can remove the element, and then use push_back.




回答3:


Remove it then append it to your list.



来源:https://stackoverflow.com/questions/4912380/move-list-element-to-the-end-in-stl

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!