gpt4 book ai didi

c++ - 带有双端队列的随机访问迭代器的无效迭代器

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:04:08 25 4
gpt4 key购买 nike

我正在阅读 Scott Meyers 的有效 STL。在项目 1 中,作者提到了如何在各种容器中进行选择,下面是我难以理解的文本片段。

Would it be helpful to have a sequence container with random access iterators where pointers and references to the data are not invalidated as long as nothing is erased and insertions take place only at the ends of the container? This is a very special case, but if it’s your case, deque is the container of your dreams. (Interestingly,deque’s iterators may be invalidated when insertions are made only at the ends of the container. deque is the only standard STL container whose iterators may be invalidated without also invalidating its pointers and references.)

我对以上文字的疑问

  1. 作者在上述上下文中所说的指针和引用是什么意思,它与迭代器有何不同?

  2. 当仅在末尾进行插入但我们仍然有有效的指针和引用时,双端队列的迭代器如何失效?

要求用简单的例子回答以上两个问题。

感谢您的宝贵时间和帮助。

最佳答案

对于第一部分,意思是这样的:

deque<int> foo(10, 1); // a deque with ten elements with value of 1
int& bar = foo.front(); // reference
int* baz = &foo.front(); // pointer
deque<int>::iterator buz = foo.begin(); // iterator
deque.push_front(0);
// At this point bar and baz are still valid, but buz may have been invalidated

对于第二部分,这里有详细介绍:

Why does push_back or push_front invalidate a deque's iterators?

关于c++ - 带有双端队列的随机访问迭代器的无效迭代器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13474281/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com