为什么我更喜欢使用向量deque [英] Why would I prefer using vector to deque

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

问题描述

由于


  1. 它们都是连续的内存容器;



  2. 为什么有人喜欢 std :: vector std :: deque

    解决方案

    deque 中的元素在内存中不是连续的; 矢量元素保证为。因此,如果你需要与需要连续数组的简单C库交互,或者如果你关心空间局部性,那么你可能更喜欢 vector 。此外,由于有一些额外的簿记,其他操作可能比它们的等价向量操作更贵。另一方面,使用向量的许多/大实例可能导致不必要的堆碎片(减慢对 new )。



    此外,如指出其他地方StackOverflow ,这里有更好的讨论: http://www.gotw.ca/gotw/054.htm


    Since

    1. they are both contiguous memory containers;
    2. feature wise, deque has almost vector has but more, since it is more efficient to insert in the front.

    Why whould anyone prefer std::vector to std::deque?

    解决方案

    Elements in a deque are not contiguous in memory; vector elements are guaranteed to be. So if you need to interact with a plain C library that needs contiguous arrays, or if you care (a lot) about spatial locality, then you might prefer vector. In addition, since there is some extra bookkeeping, other ops are probably (slightly) more expensive than their equivalent vector operations. On the other hand, using many/large instances of vector may lead to unnecessary heap fragmentation (slowing down calls to new).

    Also, as pointed out elsewhere on StackOverflow, there is more good discussion here: http://www.gotw.ca/gotw/054.htm .

    这篇关于为什么我更喜欢使用向量deque的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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