为什么喜欢std :: vector over std :: deque? [英] Why prefer std::vector over std::deque?
问题描述
看起来deque有更好的性能,但为什么大多数人使用vector of deque?
为什么大多数人使用vector而不是deque?
因为这是他们所教授的。
向量
和 deque
提供略有不同的用途。它们都可以用作对象的简单容器,如果这是所有你需要的。当学习编程C ++时,是所有大多数人需要的 - 一个桶去放置东西,得到东西和走过。
当StackOverflow被询问默认情况下我应该使用哪个容器这样的问题时,答案几乎总是向量
。这个问题通常从C ++的学习到程序的上下文中提出,在程序员提出这样一个问题的时候,他们还不知道他们不知道什么。还有很多他们还不知道。所以,我们(StackOverflow)需要一个容器,适合几乎每一个需要更好或更坏,可以在几乎任何上下文中使用,并且不要求程序员在降落之前询问所有正确的问题,接近正确的答案。此外,标准特别建议使用向量
。 向量
对于所有用途不是最佳,事实上 deque
code> vector 很多常见的用法 - 但它不是一个学习程序员,我们应该从标准的建议变化到新手C ++程序员,所以StackOverflow落在 vector
。
在学习了语法的基础知识之后,我们应该说,C ++编程背后的策略,到两个分支:那些关心更多的学习和写更好的程序,和那些没有。那些不会坚持在矢量
永远。我认为许多程序员都属于这个阵营。
那些尝试超越这个阶段开始询问其他问题的罕见程序员,像你在这里问过的问题。他们知道有很多他们还不知道,他们想开始发现这些东西。他们会快速(或不太快)发现在矢量
和 deque
之间选择时,他们没有想到的一些问题
- 我需要记忆力是否有争议?
- 我需要
- 我需要我的集合与一些古代的C语言兼容吗? 类似的函数?
然后他们真的开始考虑他们写的代码,发现更多的东西他们不知道,节拍继续...
They both have access complexity of O(1) and random insertion/removal complexity of O(n). But vector costs more when expanding because of reallocation and copy, while deque does not have this issue.
It seems deque has a better performance, but why most people use vector instead of deque?
why most people use vector instead of deque?
Because this is what they have been taught.
vector
and deque
serve slightly different purposes. They can both be used as a simply container of objects, if that's all you need. When learning to program C++, that is all most people need -- a bucket to drop stuff in to, get stuff out of, and walk over.
When StackOverflow is asked a question like "which container should I use by default," the answer is almost invariably vector
. The question is generally asked from the context of learning to program in C++, and at the point where a programmer is asking such a question, they don't yet know what they don't know. And there's a lot they don't yet know. So, we (StackOverflow) need a container that fits almost every need for better or worse, can be used in almost any context, and doesn't require that the programmer has asked all the right questions before landing on something approximating the correct answer. Furthermore, the Standard specifically recommends using vector
. vector
isn't best for all uses, and in fact deque
is better than vector
for many common uses -- but it's not so much better for a learning programmer that we should vary from the Standard's advice to newbie C++ programmers, so StackOverflow lands on vector
.
After having learned the basics of the syntax and, shall we say, the strategies behind programming in C++, programmers split in to two branches: those who care to learn more and write better programs, and those who don't. Those who don't will stick on vector
forever. I think many programmers fall in to this camp.
The rarer programmers who try to move beyond this phase start asking other questions -- questions like you've asked here. They know there is lots they don't yet know, and they want to start discovering what those things are. They will quickly (or less quickly) discover that when choosing between vector
and deque
, some questions they didn't think to ask before are:
- Do I need the memory to be contigious?
- Do I need to avoid lots of reallocations?
- Do I need to keep valid iterators after insertions?
- Do I need my collection to be compatible with some ancient C-like function?
Then they really start thinking about the code they are writing, discover yet more stuff they don't know, and the beat goes on...
这篇关于为什么喜欢std :: vector over std :: deque?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!