Deque - 为什么“储备”不存在?

时间:2022-11-07 12:17:44

The standard STL vector container has a "reserve" function to reserve uninitialized memory that can be used later to prevent reallocations.

标准STL向量容器具有“保留”功能,用于保留未初始化的内存,以后可用于防止重新分配。

How come that the other deque container hasn't it?

为什么另一个deque容器没有呢?

6 个解决方案

#1


11  

Increasing the size of a std::vector can be costly. When a vector outgrows its reserved space, the entire contents of the vector must be copied (or moved) to a larger reserve.

增加std :: vector的大小可能代价很高。当向量超出其保留空间时,必须将向量的整个内容复制(或移动)到更大的保留。

It is specifically because frequent std::vector resizing can be costly that vector::reserve() exists.

特别是因为频繁的std :: vector大小调整可能代价很高,因为vector :: reserve()存在。

Conversely, a deque can always add more memory without needing to relocate the existing elements.

相反,deque总是可以添加更多内存,而无需重新定位现有元素。

#2


6  

For vector and string, reserved space prevents later insertions at the end (up to the capacity) from invalidating iterators and references to earlier elements, by ensuring that elements don't need to be copied/moved. This relocation may also be costly.

对于向量和字符串,保留空间通过确保不需要复制/移动元素来防止最后的插入(直到容量)使迭代器和对早期元素的引用无效。这种搬迁也可能是昂贵的。

With deque and list, earlier references are never invalidated by insertions at the end, and elements aren't moved, so the need to reserve capacity does not arise.

使用deque和list,最后的引用永远不会被最后的插入无效,并且元素不会移动,因此不会出现保留容量的需要。

You might think that with vector and string, reserving space also guarantees that later insertions will not throw an exception (unless a constructor throws), since there's no need to allocate memory. You might think that the same guarantee would be useful for other sequences, and hence deque::reserve would have a possible use. There is in fact no such guarantee for vector and string, although in most (all?) implementations it's true. So this is not the intended purpose of reserve.

您可能认为使用向量和字符串,保留空间也可以保证以后的插入不会抛出异常(除非构造函数抛出),因为不需要分配内存。您可能认为相同的保证对其他序列有用,因此deque :: reserve可能会有用。实际上对矢量和字符串没有这样的保证,尽管在大多数(全部?)实现中都是如此。所以这不是储备的预期目的。

#3


2  

Quoting from C++ Reference

引用C ++参考

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.
The storage of a deque is automatically expanded and contracted as needed. Expansion of a deque is cheaper than the expansion of a std::vector because it does not involve copying of the existing elements to a new memory location.

与std :: vector相反,双端队列的元素不是连续存储的:典型的实现使用一系列单独分配的固定大小的数组。双端队列的存储会根据需要自动扩展和收缩。扩展deque比std :: vector的扩展便宜,因为它不涉及将现有元素复制到新的内存位置。

Deque can allocate new memory anywhere it wants and just point to it, unlike vectors which require a continuous block of memory to hold all their elements.

Deque可以在任何想要的地方分配新的内存并指向它,这与需要连续内存块来保存所有元素的向量不同。

#4


1  

Only vector have. There is no need of reserve function for deque, since elements not stored continuougusly and there is no need to reallocate and move elements when add, or remove elements.

只有矢量有。 deque不需要保留功能,因为元素不是连续存储的,并且在添加或删除元素时不需要重新分配和移动元素。

#5


1  

reserve implies allocation of large blocks of contiguous data (like a vector). There is nothing in the dequeue that implies contiguous storage - it's generally implemented more like a list (which you will notice also doesn't have a 'reserve' function).

reserve意味着分配大块连续数据(如向量)。在dequeue中没有任何东西暗示连续存储 - 它通常更像是一个列表(你会发现它也没有'reserve'功能)。

Thus, a 'reserve' function would make no sense.

因此,“保留”功能毫无意义。

#6


1  

There are 2 main types of memory: memories that allocate a single chunk like array and vectors, and distributed memories whose members grabs any empty location to fill in. queue and linkest list structures belong to the second type and they have some special practical advantages such that deleting a particular element does not cause a mass memory movement as opposed to arrays and vectors. Therefore they do not need to reserve any space beforehand, if they need it they just take it by connecting to tip

存在两种主要类型的存储器:分配单个块(如阵列和向量)的存储器,以及其成员抓取任何空位置以填充的分布式存储器。队列和链接列表结构属于第二类型并且它们具有一些特殊的实际优点,例如删除特定元素不会导致大量内存移动,而不是数组和向量。因此,他们不需要预先保留任何空间,如果他们需要,他们只需要连接到小费

#1


11  

Increasing the size of a std::vector can be costly. When a vector outgrows its reserved space, the entire contents of the vector must be copied (or moved) to a larger reserve.

增加std :: vector的大小可能代价很高。当向量超出其保留空间时,必须将向量的整个内容复制(或移动)到更大的保留。

It is specifically because frequent std::vector resizing can be costly that vector::reserve() exists.

特别是因为频繁的std :: vector大小调整可能代价很高,因为vector :: reserve()存在。

Conversely, a deque can always add more memory without needing to relocate the existing elements.

相反,deque总是可以添加更多内存,而无需重新定位现有元素。

#2


6  

For vector and string, reserved space prevents later insertions at the end (up to the capacity) from invalidating iterators and references to earlier elements, by ensuring that elements don't need to be copied/moved. This relocation may also be costly.

对于向量和字符串,保留空间通过确保不需要复制/移动元素来防止最后的插入(直到容量)使迭代器和对早期元素的引用无效。这种搬迁也可能是昂贵的。

With deque and list, earlier references are never invalidated by insertions at the end, and elements aren't moved, so the need to reserve capacity does not arise.

使用deque和list,最后的引用永远不会被最后的插入无效,并且元素不会移动,因此不会出现保留容量的需要。

You might think that with vector and string, reserving space also guarantees that later insertions will not throw an exception (unless a constructor throws), since there's no need to allocate memory. You might think that the same guarantee would be useful for other sequences, and hence deque::reserve would have a possible use. There is in fact no such guarantee for vector and string, although in most (all?) implementations it's true. So this is not the intended purpose of reserve.

您可能认为使用向量和字符串,保留空间也可以保证以后的插入不会抛出异常(除非构造函数抛出),因为不需要分配内存。您可能认为相同的保证对其他序列有用,因此deque :: reserve可能会有用。实际上对矢量和字符串没有这样的保证,尽管在大多数(全部?)实现中都是如此。所以这不是储备的预期目的。

#3


2  

Quoting from C++ Reference

引用C ++参考

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.
The storage of a deque is automatically expanded and contracted as needed. Expansion of a deque is cheaper than the expansion of a std::vector because it does not involve copying of the existing elements to a new memory location.

与std :: vector相反,双端队列的元素不是连续存储的:典型的实现使用一系列单独分配的固定大小的数组。双端队列的存储会根据需要自动扩展和收缩。扩展deque比std :: vector的扩展便宜,因为它不涉及将现有元素复制到新的内存位置。

Deque can allocate new memory anywhere it wants and just point to it, unlike vectors which require a continuous block of memory to hold all their elements.

Deque可以在任何想要的地方分配新的内存并指向它,这与需要连续内存块来保存所有元素的向量不同。

#4


1  

Only vector have. There is no need of reserve function for deque, since elements not stored continuougusly and there is no need to reallocate and move elements when add, or remove elements.

只有矢量有。 deque不需要保留功能,因为元素不是连续存储的,并且在添加或删除元素时不需要重新分配和移动元素。

#5


1  

reserve implies allocation of large blocks of contiguous data (like a vector). There is nothing in the dequeue that implies contiguous storage - it's generally implemented more like a list (which you will notice also doesn't have a 'reserve' function).

reserve意味着分配大块连续数据(如向量)。在dequeue中没有任何东西暗示连续存储 - 它通常更像是一个列表(你会发现它也没有'reserve'功能)。

Thus, a 'reserve' function would make no sense.

因此,“保留”功能毫无意义。

#6


1  

There are 2 main types of memory: memories that allocate a single chunk like array and vectors, and distributed memories whose members grabs any empty location to fill in. queue and linkest list structures belong to the second type and they have some special practical advantages such that deleting a particular element does not cause a mass memory movement as opposed to arrays and vectors. Therefore they do not need to reserve any space beforehand, if they need it they just take it by connecting to tip

存在两种主要类型的存储器:分配单个块(如阵列和向量)的存储器,以及其成员抓取任何空位置以填充的分布式存储器。队列和链接列表结构属于第二类型并且它们具有一些特殊的实际优点,例如删除特定元素不会导致大量内存移动,而不是数组和向量。因此,他们不需要预先保留任何空间,如果他们需要,他们只需要连接到小费