什么时候更喜欢使用std :: list< T>而不是std :: vector< T> [英] When do you prefer using std::list<T> instead of std::vector<T>?

查看:109
本文介绍了什么时候更喜欢使用std :: list< T>而不是std :: vector< T>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从来没有使用过 std :: list< T> 。我想知道当人们使用它,当我们已经有 std :: vector< T> 这就像是具有连续内存的数组。 std :: vector 似乎是一个完美的选择,当我们需要顺序容器!



所以我的问题是




  • 您最喜欢 std :: list code> std :: vector ?和为什么?



  • 如果有性能考虑,请列出详细说明/信息。



    如果可能,请引用一些参考资料,以支持您的答案。

    解决方案

    列表更适合插入或删除中间任何地方,向量最好插入到结尾。



    向量也更好



    这是一个实现它们的方式的伪像。





    如果变化的数量是实质性的(相比于访问)或者变化集中在结束,



    例如,在程序启动时读取一个集合,几乎不会更改它(或者如果改变不断添加到结尾),这将是一个好的候选人向量。



    另一方面,电话簿应用程序流行和fickle摇滚明星,我会寻找一个列表。实际上,我会寻找一个数据库连接,但这是我能在短时间内提出的最好的例子: - )



    至于引用,最新的C + + 0x草稿状态部分(23.3.4,列表):


    列表是一个序列容器,支持双向迭代器,在序列内的任何位置插入和擦除操作,自动处理存储管理。与vector和deques不同,不支持快速随机访问列表元素。


    第23.3.5节(向量):


    向量是一个支持随机存取迭代器的序列容器。此外,它支持(摊销)在结束时的恒定时间插入和擦除操作;插入和擦除在中间取线性时间。



    I've never used std::list<T> myself. I was wondering when people use it when we already have std::vector<T> which is just like arrays with contiguous memory. std::vector seems like a perfect choice when we need sequential container!

    So my question is

    • When exactly do you prefer std::list over std::vector? and why exactly?
    • When do you prefer std::vector over std::list? and why?

    If there is performance consideration, then please list them too with detail explanation/information.

    If possible, quote some references also, to support your answer.

    解决方案

    Lists are better for inserting or deleting anywhere in the middle, vectors are better for inserting at the end.

    Vectors are also better for accessing elements.

    This is an artefact of the way they're implemented.

    So, if a collection changes very little (compared to accesses) or the changes are concentrated at the end, I'd use a vector.

    If the number of changes is substantial (compared to accesses) and they're not at the ends, I'd use a list.

    By way of example, reading in a collection at program startup and hardly ever changing it (or if the changes are onlt adding to the end), this would be a good candidate for a vector.

    On the other hand, a phone book application for a particularly popular and fickle rock star, I'd be looking towards a list. Actually I'd be looking toward a database connection but that was the best example I could come up with at short notice :-)

    As to references, the latest C++0x draft states in part (23.3.4, lists):

    A list is a sequence container that supports bidirectional iterators and allows constant time insert and erase operations anywhere within the sequence, with storage management handled automatically. Unlike vectors and deques, fast random access to list elements is not supported.

    Section 23.3.5 (on vectors):

    A vector is a sequence container that supports random access iterators. In addition, it supports (amortized) constant time insert and erase operations at the end; insert and erase in the middle take linear time.

    这篇关于什么时候更喜欢使用std :: list&lt; T&gt;而不是std :: vector&lt; T&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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