为什么喜欢std :: vector over std :: deque? [英] Why prefer std::vector over std::deque?

查看:104
本文介绍了为什么喜欢std :: vector over std :: deque?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它们都具有O(1)的访问复杂性和O(n)的随机插入/移除复杂性。但是向量在扩展时会因为重新分配和复制而花费更多,而deque没有这个问题。



看起来deque有更好的性能,但为什么大多数人使用vector of deque?

解决方案

 为什么大多数人使用vector而不是deque? 

因为这是他们所教授的。



向量 deque 提供略有不同的用途。它们都可以用作对象的简单容器,如果这是所有你需要的。当学习编程C ++时,是所有大多数人需要的 - 一个桶去放置东西,得到东西和走过。



当StackOverflow被询问默认情况下我应该使用哪个容器这样的问题时,答案几乎总是向量。这个问题通常从C ++的学习到程序的上下文中提出,在程序员提出这样一个问题的时候,他们还不知道他们不知道什么。还有很多他们还不知道。所以,我们(StackOverflow)需要一个容器,适合几乎每一个需要更好或更坏,可以在几乎任何上下文中使用,并且不要求程序员在降落之前询问所有正确的问题,接近正确的答案。此外,标准特别建议使用向量向量对于所有用途不是最佳,事实上 deque code> vector 很多常见的用法 - 但它不是一个学习程序员,我们应该从标准的建议变化到新手C ++程序员,所以StackOverflow落在 vector



在学习了语法的基础知识之后,我们应该说,C ++编程背后的策略,到两个分支:那些关心更多的学习和写更好的程序,和那些没有。那些不会坚持在矢量永远。我认为许多程序员都属于这个阵营。



那些尝试超越这个阶段开始询问其他问题的罕见程序员,像你在这里问过的问题。他们知道有很多他们还不知道,他们想开始发现这些东西。他们会快速(或不太快)发现在矢量 deque 之间选择时,他们没有想到的一些问题


  1. 我需要记忆力是否有争议?

  2. 我需要

  3. 我需要我的集合与一些古代的C语言兼容吗?
  4. 类似的函数?

然后他们真的开始考虑他们写的代码,发现更多的东西他们不知道,节拍继续...


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:

  1. Do I need the memory to be contigious?
  2. Do I need to avoid lots of reallocations?
  3. Do I need to keep valid iterators after insertions?
  4. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆