<i id='SvYDk'><tr id='SvYDk'><dt id='SvYDk'><q id='SvYDk'><span id='SvYDk'><b id='SvYDk'><form id='SvYDk'><ins id='SvYDk'></ins><ul id='SvYDk'></ul><sub id='SvYDk'></sub></form><legend id='SvYDk'></legend><bdo id='SvYDk'><pre id='SvYDk'><center id='SvYDk'></center></pre></bdo></b><th id='SvYDk'></th></span></q></dt></tr></i><div id='SvYDk'><tfoot id='SvYDk'></tfoot><dl id='SvYDk'><fieldset id='SvYDk'></fieldset></dl></div>
      • <bdo id='SvYDk'></bdo><ul id='SvYDk'></ul>

      <small id='SvYDk'></small><noframes id='SvYDk'>

      <tfoot id='SvYDk'></tfoot>
    1. <legend id='SvYDk'><style id='SvYDk'><dir id='SvYDk'><q id='SvYDk'></q></dir></style></legend>

      如何设置 std::vector 的初始大小?

      How to set initial size of std::vector?(如何设置 std::vector 的初始大小?)
      <tfoot id='bYEmq'></tfoot>
      <legend id='bYEmq'><style id='bYEmq'><dir id='bYEmq'><q id='bYEmq'></q></dir></style></legend>
            <tbody id='bYEmq'></tbody>

              <small id='bYEmq'></small><noframes id='bYEmq'>

              <i id='bYEmq'><tr id='bYEmq'><dt id='bYEmq'><q id='bYEmq'><span id='bYEmq'><b id='bYEmq'><form id='bYEmq'><ins id='bYEmq'></ins><ul id='bYEmq'></ul><sub id='bYEmq'></sub></form><legend id='bYEmq'></legend><bdo id='bYEmq'><pre id='bYEmq'><center id='bYEmq'></center></pre></bdo></b><th id='bYEmq'></th></span></q></dt></tr></i><div id='bYEmq'><tfoot id='bYEmq'></tfoot><dl id='bYEmq'><fieldset id='bYEmq'></fieldset></dl></div>
              • <bdo id='bYEmq'></bdo><ul id='bYEmq'></ul>
                本文介绍了如何设置 std::vector 的初始大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有一个 vector 并且我在 vector 中放入了很多项目,我需要快速访问,所以我不使用 list.如何设置vector的初始大小(例如设置为20 000个位置,以免插入new时复制)?

                I have a vector<CustomClass*> and I put a lot of items in the vector and I need fast access, so I don't use list. How to set initial size of vector (for example to be 20 000 places, so to avoid copy when I insert new)?

                推荐答案

                std::vector<CustomClass *> whatever(20000);
                

                或:

                std::vector<CustomClass *> whatever;
                whatever.reserve(20000);
                

                前者设置数组的实际大小——即,使其成为一个包含 20000 个指针的向量.后者将向量留空,但为 20000 个指针保留空间,因此您可以插入(最多)这么多而无需重新分配.

                The former sets the actual size of the array -- i.e., makes it a vector of 20000 pointers. The latter leaves the vector empty, but reserves space for 20000 pointers, so you can insert (up to) that many without it having to reallocate.

                至少根据我的经验,这两者中的任何一个对性能产生巨大差异是相当不寻常的——但在某些情况下,任何一个都会影响正确性.特别是,只要不发生重新分配,向量中的迭代器就可以保证保持有效,并且一旦您设置了大小/保留空间,就可以保证只要您不重新分配就不会发生任何重新分配t 增加超过这个大小.

                At least in my experience, it's fairly unusual for either of these to make a huge difference in performance--but either can affect correctness under some circumstances. In particular, as long as no reallocation takes place, iterators into the vector are guaranteed to remain valid, and once you've set the size/reserved space, you're guaranteed there won't be any reallocations as long as you don't increase the size beyond that.

                这篇关于如何设置 std::vector 的初始大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Unable to access non-const member functions of objects in C++ std::set(无法访问 C++ std::set 中对象的非常量成员函数)
                How should a size-limited stl-like container be implemented?(应该如何实现大小受限的 stl 类容器?)
                Constructing std::function argument from lambda(从 lambda 构造 std::function 参数)
                STL BigInt class implementation(STL BigInt 类实现)
                Sync is unreliable using std::atomic and std::condition_variable(使用 std::atomic 和 std::condition_variable 同步不可靠)
                Move list element to the end in STL(在 STL 中将列表元素移动到末尾)
                  <legend id='QMVzb'><style id='QMVzb'><dir id='QMVzb'><q id='QMVzb'></q></dir></style></legend>

                    <tbody id='QMVzb'></tbody>

                      <small id='QMVzb'></small><noframes id='QMVzb'>

                      • <bdo id='QMVzb'></bdo><ul id='QMVzb'></ul>

                      • <i id='QMVzb'><tr id='QMVzb'><dt id='QMVzb'><q id='QMVzb'><span id='QMVzb'><b id='QMVzb'><form id='QMVzb'><ins id='QMVzb'></ins><ul id='QMVzb'></ul><sub id='QMVzb'></sub></form><legend id='QMVzb'></legend><bdo id='QMVzb'><pre id='QMVzb'><center id='QMVzb'></center></pre></bdo></b><th id='QMVzb'></th></span></q></dt></tr></i><div id='QMVzb'><tfoot id='QMVzb'></tfoot><dl id='QMVzb'><fieldset id='QMVzb'></fieldset></dl></div>

                          <tfoot id='QMVzb'></tfoot>