STD :: Deque是否连续内存容器?
std::deque 是否是连续的内存容器?
Scott Meyers 的著名著作《Effective STL》如下所述
连续内存容器(也称为基于数组的容器)将其元素存储在一个或多个(动态分配的)内存块中,每个块保存多个容器元素。如果插入新元素或现有元素被擦除后,同一内存块中的其他元素必须向上或向下移动,以便为新元素腾出空间或填充以前被擦除元素占据的空间,这种移动会影响性能(参见第 5 条和第 14 条)。和异常安全(如我们很快就会看到)。标准的连续内存容器是向量、字符串和双端队列。非标准绳索也是连续内存容器。
但是您可以在 cppreference.com 中找到相反的解释。
与 std::vector 相反,双端队列的元素不是连续存储的:典型的实现使用一系列单独分配的固定大小数组,并进行额外的簿记,这意味着对双端队列的索引访问必须执行两个指针取消引用,与仅执行一次的向量索引访问相比。
哪一个是真的?
std::deque is contiguous memory container or not ?
The famous book Effective STL by Scott Meyers says like below
Contiguous-memory containers (also known as array-based containers] store their elements in one or more (dynamically allocated) chunks of memory, each chunk holding more than one container element. If a new element is inserted or an existing element is erased, other elements in the same memory chunk have to be shifted up or down to make room for the new element or to fill the space formerly occupied by the erased element. This kind of movement affects both performance (see Items 5 and 14) and exception safety (as we'll soon see). The standard contiguous-memory containers are vector, string, and deque. The nonstandard rope is also a contiguous-memory container.
But you can find opposite explanation in cppreference.com
As opposed to std::vector, the elements of a deque are not stored contiguously: typical implementations use a sequence of individually allocated fixed-size arrays, with additional bookkeeping, which means indexed access to deque must perform two pointer dereferences, compared to vector's indexed access which performs only one.
Which one is true ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
两个引用之间没有冲突。 Scott 使用术语“连续容器”的含义比您在其他地方看到的更广泛。
斯科特写道(强调我的):
正如 cppref 中的引用所述,
std::deque
使用多个数组来存储每个数组中的元素是连续的。Scott 并不声称 std::deque 中的所有元素在一个块中都是连续的。记忆。There is no conflict between the two quotes. Scott uses the term "contiguous container" in a sense a little wider than you might have seen it used elsewhere.
Scott writes (emphasize mine):
As the quote from cppref states,
std::deque
uses multiple arrays to store the elements. Within each array the elements are contiguous. Scott does not claim that all elements in astd::deque
are contiguous in one chunk of memory.