std::deque 是否是连续的内存容器?

std::deque is contiguous memory container or not?

std::deque 是否是连续的内存容器?

Scott Meyers 的名著 Effective STL 如下所述

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.

但是你可以在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.

哪个是真的?

两个引用之间没有冲突。 Scott 使用术语“连续容器”的意义比您在其他地方看到的要宽泛一些。

斯科特写道(强调我的):

Contiguous-memory containers (also known as array-based containers] store their elements in one or more (dynamically allocated) chunks of memory, [...]

正如 cppref 中的引文所述,std::deque 使用多个数组来存储元素。在每个数组中,元素都是连续的。 Scott 并未声称 std::deque 中的所有元素在一块内存中都是连续的。