C ++ STL容器:deque和list之间有什么区别? [英] C++ STL containers: what's the difference between deque and list?

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

问题描述

两者之间有什么区别?我的意思是方法都是一样的。因此,对于用户,他们工作相同。

What is the difference between the two? I mean the methods are all the same. So, for a user, they work identically.

是正确的

推荐答案

仍然非常有用) SGI STL deque

From the (dated but still very useful) SGI STL summary of deque:


非常像一个向量:它是一个序列,支持对元素的随机访问,在序列末尾的元素的常量时间插入和删除,以及在中间的元素的线性时间插入和删除。

A deque is very much like a vector: like vector, it is a sequence that supports random access to elements, constant time insertion and removal of elements at the end of the sequence, and linear time insertion and removal of elements in the middle.

deque与向量不同的主要方式是deque也支持在序列开头的恒定时间插入和删除元素。此外,deque没有类似于vector的capacity()和reserve()的任何成员函数,并且不提供与那些成员函数相关联的迭代器有效性的任何保证。

The main way in which deque differs from vector is that deque also supports constant time insertion and removal of elements at the beginning of the sequence. Additionally, deque does not have any member functions analogous to vector's capacity() and reserve(), and does not provide any of the guarantees on iterator validity that are associated with those member functions.

以下是 列表 来自同一网站:

Here's the summary on list from the same site:


列表是一个双向链表。也就是说,它是支持向前和向后遍历以及在开始或结束或在中间的元素的(摊销)恒定时间插入和移除的序列。列表有一个重要的属性,插入和拼接不会使迭代器无效列表元素,甚至删除无效的指向被删除的元素的迭代器。迭代器的顺序可以改变(也就是说,list ::迭代器在列表操作之后可能有一个不同的前导或后继,而不是以前),但是迭代器本身不会失效或指向不同的元素,除非失效或突变是显式的。

A list is a doubly linked list. That is, it is a Sequence that supports both forward and backward traversal, and (amortized) constant time insertion and removal of elements at the beginning or the end, or in the middle. Lists have the important property that insertion and splicing do not invalidate iterators to list elements, and that even removal invalidates only the iterators that point to the elements that are removed. The ordering of iterators may be changed (that is, list::iterator might have a different predecessor or successor after a list operation than it did before), but the iterators themselves will not be invalidated or made to point to different elements unless that invalidation or mutation is explicit.

总之,容器可能有共享例程,但是这些例程的时间保证不同于容器容器。当考虑将这些容器中的哪一个用于任务时,这是非常重要的:考虑容器将被最频繁使用的方​​式(例如,更多的用于搜索而不是插入/删除)指导你到正确的容器。

In summary the containers may have shared routines but the time guarantees for those routines differ from container to container. This is very important when considering which of these containers to use for a task: taking into account how the container will be most frequently used (e.g., more for searching than for insertion/deletion) goes a long way in directing you to the right container.

这篇关于C ++ STL容器:deque和list之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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