gpt4 book ai didi

c++ - 我可以在 boost 侵入列表中使用双端队列项目吗

转载 作者:太空宇宙 更新时间:2023-11-04 14:22:01 27 4
gpt4 key购买 nike

我有带有侵入式列表 Hook 的对象。我想通过将它们保存在标准集合中来管理内存。然后我使用侵入式集合对对象执行操作(排序等...)。

intrusive::list<MyObject> intrusiveList;
vector<MyObject> stdVector;
stdVector.push_back(MyObject());
intrusiveList.push_back(stdVector.front());

vector 不是一个好的选择(除非您可以预先调整大小),因为它可能会将对象复制到不同的内存,这会使任何侵入式 Hook 失效。双端队列呢?对于那些不熟悉 boost::intrusive 的人来说,这个问题实际上可以归结为:双端队列能否将一个对象重新复制到不同的内存中,或者它是否保证在双端队列的生命周期内保持在同一内存中?我知道有一些关于 back() 和 front() 的保证,但它不能保证所有迭代器永远不会失效,所以我有点困惑。

最佳答案

只有在双端队列中间插入或删除时,双端队列才会复制对象。您可以在两端添加或删除,而无需移动任何对象。

相信 iterators 可能会失效,即使每个成员的地址保持不变。双端队列具有两级结构,一些内部数据可能会重新分配,从而使双端队列中的迭代器无效。


更准确地说:

An insertion in the middle of the deque invalidates all the iterators and references to elements of the deque. An insertion at either end of the deque invalidates all the iterators to the deque, but has no effect on the validity of references to elements of the deque.

关于c++ - 我可以在 boost 侵入列表中使用双端队列项目吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6762381/

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