C++在push_front()之后双端队列的迭代器失效

发布于 2024-08-09 04:03:26 字数 592 浏览 2 评论 0原文

刚才,我正在读 Josuttis 的 STL 书。

据我所知——c++向量是一个可以重新分配的c数组。所以,我明白为什么在 push_back() 之后所有迭代器和引用都会变得无效。

但我的问题是关于 std::deque 的。据我所知,它是大块数组(c 数组的 c 数组)。因此,push_front() 在开头插入元素,如果没有空间,双端队列分配新块,并将元素放置在分配块的末尾。

在中间的 insert() 之后,所有引用和迭代器都变得无效,我明白为什么——所有元素都被移动。 但我真的误解了这句话“...在push_back()和push_front()之后所有引用保持有效,但迭代器不”(相同的短语可以在@ standard:23.2.2.3找到)

这是什么意思?!如果引用有效,则双端队列无法重新分配(==移动)其元素。那么为什么迭代器会失效呢?为什么我在插入非移动元素后无法使用它们?或者这句话的意思是,我无法确定迭代器是否等于 begin() 或 end() 以及溢出?

另外,我想提一下,在擦除()之后,所有迭代器和引用都保持有效(除了被擦除的:-))。

PS:请不要以“标准”形式回答:“它不能使用,因为标准是这么说的”。 我想了解为什么,会发生什么。

Just now, I'm reading Josuttis' STL book.

As far as I know -- c++ vector is a c-array that can be reallocated. So, I understand, why after push_back() all iterators and references can become invalid.

But my question is about std::deque. As I know it is array of large blocks (c-array of c-arrays). So push_front() inserts element at the beginning and if there is no space, deque allocates new block, and places the element at allocated block's end.

After insert() in the middle all references and iterators become invalid and I understand why -- all elements are moved.
But I really misunderstand the phrase "...after push_back() and push_front() all references stays valid, but iterators don't" (same phrase can be found @ standard:23.2.2.3)

What does it mean?! If references are valid, than deque couldn't reallocate (== move) its elements. So why iterators become invalid? Why can't I use them after non-moving-elements insertion? Or does the phrase mean, that I can't be sure about iterators equality to begin() or end() and overflow?

Also, I wanna mention, that after erase() all iterators and references stay valid (except the erased one :-) ).

PS: please don't answer in "standard" form: "it can't be used because THE STANDARD says so".
I wanna understand why, what can happen.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

野味少女 2024-08-16 04:03:26

我认为迭代器失效但引用不失效的原因可能是因为可能存在指向存储元素的双端队列页面的指针数组的双端队列实现。对双端队列中元素的引用将直接引用“页面”中的元素。然而,双端队列中的迭代器可能依赖于指向各个页面的指针向量。

将新元素插入双端队列的一端或另一端永远不需要重新分配和移动现有数据页,但可能需要添加(因此重新分配和复制)页指针数组,从而使依赖于前一个数据页的任何迭代器无效页指针数组。

Array of pointers           
(if this grows                 Data Pages
 and gets copied,           (these never move
 iterators are invalid)      due to insert at ends)
-----------------          --------------------

 +----------+               +----------+
 |         -+-------------->|          |
 +----------+               +----------+
 |         -+---------+     |          |
 +----------+         |     +----------+
 |         -+---+     |     |          |
 +----------+   |     |     +----------+ 
                |     |
                |     |
                |     |
                |     |     +----------+
                |     +---->|          |
                |           +----------+
                |           |          |
                |           +----------+
                |           |          |
                |           +----------+ 
                |           
                |           +----------+
                +---------->|          |
                            +----------+
                            |          |
                            +----------+
                            |          |
                            +----------+ 

I think that the reason iterators get invalidated but references not might be because of the possible deque implementation of an array of pointers to the deque's pages that store the elements. A reference to an element in a deque will refer directly to the element in a 'page'. However, an iterator into the deque might be dependant on the vector of pointers that point to the various pages.

Inserting a new element into a deque at one or another end will never require reallocating and moving exsting data pages, but it might require adding to (and therefore reallocating & copying) the array of page pointers, invalidating any iterators that depended on the previous array of page pointers.

Array of pointers           
(if this grows                 Data Pages
 and gets copied,           (these never move
 iterators are invalid)      due to insert at ends)
-----------------          --------------------

 +----------+               +----------+
 |         -+-------------->|          |
 +----------+               +----------+
 |         -+---------+     |          |
 +----------+         |     +----------+
 |         -+---+     |     |          |
 +----------+   |     |     +----------+ 
                |     |
                |     |
                |     |
                |     |     +----------+
                |     +---->|          |
                |           +----------+
                |           |          |
                |           +----------+
                |           |          |
                |           +----------+ 
                |           
                |           +----------+
                +---------->|          |
                            +----------+
                            |          |
                            +----------+
                            |          |
                            +----------+ 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文