C++ vector和deque的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22068188/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The difference between vector and deque
提问by rajenpandit
As vector
and deque
both provides a function to push_back
the element at the last.
Asvector
和deque
bothpush_back
最后为元素提供了一个函数。
where deque
also provides a function push_front
to insert the element at the beginning, which is bit costly in case of vector
.
wheredeque
还提供了push_front
在开头插入元素的函数,在vector
.
My question is when we can achieve the same functionality (push_back
) of vector
by using deque
, then why vector
is required?
我的问题是当我们可以通过使用实现相同的功能 ( push_back
) 时,为什么需要? vector
deque
vector
回答by paxdiablo
One main difference between vectors and deques is that the latter allows efficient insertion at the frontof the structure as well as the back.
向量和双端队列之间的一个主要区别是后者允许在结构的前面和后面有效插入。
Deques also do not guarantee that their elements are contiguous in memory so the at-style operator (indexing) may not be as efficient.
Deques 也不保证它们的元素在内存中是连续的,因此 at 风格的运算符(索引)可能效率不高。
Note that the difference is unlikely to matter in practice for smaller collections but would generally become more important if, for example, the collection size increases or you're modifying it manytimes per second.
请注意,对于较小的集合,这种差异在实践中不太可能重要,但如果例如集合大小增加或您每秒对其进行多次修改,则差异通常会变得更加重要。
回答by James Kanze
Performance, mainly. An std::deque
has all of the
functionality of std::vector
, at least for normal use, but
indexing and iterating over it will typically be somewhat
slower; the same may hold for appending at the end, if you've
used reserve
. And of course, std::vector
is the default
container, using anything else will suggest to the reader that
you have special requirements.
性能,主要是。Anstd::deque
具有 的所有功能std::vector
,至少对于正常使用而言,但对其进行索引和迭代通常会稍微慢一些;如果您使用了reserve
. 当然,std::vector
是默认容器,使用其他任何东西都会向读者暗示您有特殊要求。
std::vector
also guarantees contiguity, so it (and only it)
can be used to interface legacy functions which require a T*
or a T const*
.
std::vector
还保证连续性,因此它(并且仅它)可用于接口需要 aT*
或 a 的遗留函数T const*
。
I might add that the one time I actually had a performance
issue, and measured, std::vector
was faster than std::deque
,
despitethe fact that I was regularly removing elements from
the front (using the container as a queue, pushing at the back,
and popping at the front). I don't know if that generalizes,
however; in my case, the queue was relatively short (never more
than about 15 elements, and usually many less), and the contents
were char
, which is extremely cheap to copy. But in general,
I'd use std::vector
even if I needed to remove elements from
the front, if only because of its better locality. I'd probably
only consider std::deque
if I expected thousands of elements,
which were expensive to copy.
我可能会补充说,有一次我实际上遇到了性能问题并进行了测量,但std::vector
比 快std::deque
,
尽管我经常从前面删除元素(使用容器作为队列,在后面推,并在后面弹出)正面)。但是,我不知道这是否可以概括;就我而言,队列相对较短(从不超过大约 15 个元素,通常更少),内容为char
,复制起来非常便宜。但总的来说,std::vector
即使我需要从前面删除元素,我也会使用,因为它更好的局部性。我可能只会考虑std::deque
是否需要数千个元素,而这些元素的复制成本很高。
回答by Marius Bancila
std::deque
is a double-ended queue. It provides efficient insertion and deletion of elements at the beginning also, not just at the end, as std::vector
does. Vectors are guaranteed to store the elements in a contiguous storage, therefore you can access its elements by index/offset. std::deque
does not offer this guarantee.
std::deque
是一个双端队列。它也提供了有效的元素插入和删除,而不仅仅是在结尾,就像std::vector
那样。向量保证将元素存储在连续存储中,因此您可以通过索引/偏移量访问其元素。std::deque
不提供此保证。