简体   繁体   English

C ++:如何跟踪指向STL列表元素的指针?

[英]C++: how to track a pointer to a STL list element?

I would like to track a pointer to a list element for the next read access. 我想跟踪指向下一个读访问的列表元素的指针。 This pointer would be advanced every time the list is read. 每次读取列表时,该指针都会前进。 Would it be bad practice to cache an iterator to the list as a member variable, assuming I take proper precautions when deleting list elements? 假设我在删除列表元素时采取了适当的预防措施,将迭代器作为成员变量缓存到列表是不好的做法?

An iterator to a list element remains valid until the element it refers to is removed. 列表元素的迭代器保持有效,直到它引用的元素被删除。 This is guaranteed by the standard. 这是标准的保证。

There is no problem with caching an iterator as long as you make sure you don't remove elements from the list or refresh the cached iterator when you do. 只要确保不从列表中删除元素或在执行时刷新缓存的迭代器,缓存迭代器就没有问题。

Iterators are meant to be used. 迭代器是要使用的。 They aren't just for looping over each element in a for-loop. 它们不仅仅用于循环for循环中的每个元素。 All you have to take into account is the rules for iterator invalidation. 所有你必须考虑的是迭代器失效的规则。

std::vector iterators can be invalidated by any operation that inserts elements into the list. 任何将元素插入列表的操作都可以使std::vector迭代器无效。 The iterators for all elements beyond the point of insertion are invalidated, and all iterators are invalidated if the insertion operation causes an increase in capacity. 超出插入点的所有元素的迭代器都将失效,并且如果插入操作导致容量增加,则所有迭代器都将失效。 An operation that removes an element from the vector will invalidate any iterator after the point of removal. 从向量中删除元素的操作将在删除点之后使任何迭代器无效。

std::deque iterators are invalidated by any operation that adds or removes elements from anywhere in the deque. 任何在双端队列中的任何位置添加或删除元素的操作都会使std::deque迭代器无效。 So it's probably not a good idea to keep these around very long. 因此,将这些保持很长时间可能不是一个好主意。

std::list , std::set , and std::map iterators are only invalidated by the specific removal of the particular element that the iterator refers to. std::liststd::setstd::map迭代器仅通过特定删除迭代器引用的特定元素而失效。 These are the longest-lived of the iterator types. 这些是迭代器类型中寿命最长的。

As long as you keep these rules in mind, feel free to store these iterators all you want. 只要记住这些规则,就可以随意存储这些迭代器。 It certainly isn't bad form to store std::list iterators, as long as you can be sure that that particular element isn't going anywhere. 存储std::list迭代器肯定是不错的形式,只要你能确定那个特定的元素不会去任何地方。

The only way you're going to be able to properly advance though an STL std::list<T> in a platform and compiler independent way is through the use of a std::list<T>::iterator or std::list<T>::const_iterator , so that's really your only option unless you're planning on implementing your own linked-list. 通过使用std::list<T>::iteratorstd::list<T>::const_iterator能够通过平台和编译器独立方式正确推进STL std::list<T>的唯一方法是std::list<T>::const_iterator ,除非你计划实现自己的链表,否则这是你唯一的选择。 Per the standard, as others here have posted, an iterator to a std::list element will remain valid until that element is removed from the list. 根据标准,正如其他人发布的那样, std::list元素的迭代器将保持有效,直到从列表中删除该元素。

您所询问的内容称为“输入迭代器”,快速搜索将为您提供高质量的实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM