将std :: list改变元素内存中的位置? [英] Will std::list alter the position in memory of elements?

查看:973
本文介绍了将std :: list改变元素内存中的位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我通过指针获取对std :: list中的元素的引用,那么只要元素没有被删除,那个指针就会保持有效,否则我每次要访问的时候都必须遍历列表元素?

If I grab a reference to an element in a std::list via a pointer, will that pointer remain valid so long as the element is not deleted, or would I have to traverse the list each time I want to access said element?

基本上std :: list会在添加或删除其他元素时移动元素吗?

Basically does std::list move elements around when other elements are added or removed?

怀疑不是...

推荐答案

根据 C ++引用 insert()方法对迭代器的有效性没有影响, a href =http://www.cplusplus.com/reference/list/list/remove/ =nofollow> remove 方法只会影响删除元素的迭代器的有效性。我怀疑您希望围绕迭代器指向列表中的元素,而不是指针。

According to the C++ reference, the insert() method has no effect on the validity of iterators, and the remove method only affects the validity of the removed elements' iterators. I suspect you want to keep around iterators pointing to the elements in the list, rather than pointers.

这篇关于将std :: list改变元素内存中的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆