Why would I prefer using vector to deque(为什么我更喜欢使用 vector 来 deque)
问题描述
自从
- 它们都是连续的内存容器;
- 在功能方面,deque 几乎拥有 vector 的所有功能,但更多,因为在前面插入效率更高.
为什么有人更喜欢 std::vector 而不是 std::deque?
Why whould anyone prefer std::vector to std::deque?
推荐答案
deque 中的元素在内存中不连续;vector 元素保证是.因此,如果您需要与需要连续数组的普通 C 库进行交互,或者如果您(非常)关心空间局部性,那么您可能更喜欢 vector.此外,由于有一些额外的簿记,其他操作可能(略)比其等效的 vector 操作昂贵.另一方面,使用多个/大型 vector 实例可能会导致不必要的堆碎片(减慢对 new 的调用).
Elements in a deque are not contiguous in memory; vector elements are guaranteed to be. So if you need to interact with a plain C library that needs contiguous arrays, or if you care (a lot) about spatial locality, then you might prefer vector. In addition, since there is some extra bookkeeping, other ops are probably (slightly) more expensive than their equivalent vector operations. On the other hand, using many/large instances of vector may lead to unnecessary heap fragmentation (slowing down calls to new).
此外,正如 StackOverflow 上的其他地方所指出的,这里有更多很好的讨论:http://www.gotw.ca/gotw/054.htm.
Also, as pointed out elsewhere on StackOverflow, there is more good discussion here: http://www.gotw.ca/gotw/054.htm .
这篇关于为什么我更喜欢使用 vector 来 deque的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为什么我更喜欢使用 vector 来 deque
- 如何对自定义类的向量使用std::find()? 2022-11-07
- STL 中有 dereference_iterator 吗? 2022-01-01
- C++ 协变模板 2021-01-01
- 从python回调到c++的选项 2022-11-16
- 近似搜索的工作原理 2021-01-01
- 一起使用 MPI 和 OpenCV 时出现分段错误 2022-01-01
- 静态初始化顺序失败 2022-01-01
- 与 int by int 相比,为什么执行 float by float 矩阵乘法更快? 2021-01-01
- 使用/clr 时出现 LNK2022 错误 2022-01-01
- Stroustrup 的 Simple_window.h 2022-01-01
