vector和deque之间的区别 [英] The difference between vector and deque

查看:78
本文介绍了vector和deque之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于 vector deque 都提供了 push_back 最后的元素。

As vector and deque both provides a function to push_back the element at the last.

其中 deque 还提供了 push_front 插入功能开头的元素,在 vector 的情况下会有点昂贵。

where deque also provides a function push_front to insert the element at the beginning, which is bit costly in case of vector.

我的问题是何时可以实现与 vector相同的功能( push_back 使用 deque ,那么为什么需要 vector

My question is when we can achieve the same functionality (push_back) of vector by using deque, then why vector is required?

推荐答案

向量和双端双引号之间的主要区别是后者允许在前端处高效插入

One main difference between vectors and deques is that the latter allows efficient insertion at the front of the structure as well as the back.

双端队列也不保证其元素在内存中是连续的,因此at风格的运算符(索引)可能不那么有效。

Deques also do not guarantee that their elements are contiguous in memory so the at-style operator (indexing) may not be as efficient.

请注意,对于较小的收藏夹,这种差异实际上不太重要,但是例如,如果收藏夹大小增加或您正在对其进行修改,则通常会变得更重要每秒次。

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 many times per second.

这篇关于vector和deque之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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