<small id='8p5BA'></small><noframes id='8p5BA'>

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

      <bdo id='8p5BA'></bdo><ul id='8p5BA'></ul>
  1. <legend id='8p5BA'><style id='8p5BA'><dir id='8p5BA'><q id='8p5BA'></q></dir></style></legend>
  2. <tfoot id='8p5BA'></tfoot>
    1. 调用 std::vector::clear() 会将 std::vector::capacity() 设置

      时间:2023-09-15
        <tbody id='nQ7Om'></tbody>
      • <i id='nQ7Om'><tr id='nQ7Om'><dt id='nQ7Om'><q id='nQ7Om'><span id='nQ7Om'><b id='nQ7Om'><form id='nQ7Om'><ins id='nQ7Om'></ins><ul id='nQ7Om'></ul><sub id='nQ7Om'></sub></form><legend id='nQ7Om'></legend><bdo id='nQ7Om'><pre id='nQ7Om'><center id='nQ7Om'></center></pre></bdo></b><th id='nQ7Om'></th></span></q></dt></tr></i><div id='nQ7Om'><tfoot id='nQ7Om'></tfoot><dl id='nQ7Om'><fieldset id='nQ7Om'></fieldset></dl></div>

              <bdo id='nQ7Om'></bdo><ul id='nQ7Om'></ul>
              <tfoot id='nQ7Om'></tfoot>

              <legend id='nQ7Om'><style id='nQ7Om'><dir id='nQ7Om'><q id='nQ7Om'></q></dir></style></legend>
            • <small id='nQ7Om'></small><noframes id='nQ7Om'>

              • 本文介绍了调用 std::vector::clear() 会将 std::vector::capacity() 设置为零吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                如果我在向量上使用 .reserve(items),向量将分配足够的内存来让我猜测我需要的项目数量.

                If I use .reserve(items) on a vector, the vector will allocate enough memory for my guess of the number of items that I'll need.

                如果我以后使用 .clear(),这只是清除向量还是保存我之前定义的储备?

                If I later on use .clear(), will that just clear the vector or save my earlier defined reserve?

                谢谢.

                推荐答案

                指定 std::vector::clear() 影响大小.它可能不会影响容量.要重置容量,请使用交换技巧:

                It is specified that std::vector<T>::clear() affects the size. It might not affect the capacity. For resetting the capacity, use the swap trick:

                    std::vector<int> v1;
                
                    // somehow increase capacity
                
                    std::vector<int>().swap(v1);
                

                注意:由于这个旧答案仍然得到赞成(因此人们阅读它),我觉得有必要添加C++11 添加了 std::vector<...>::shrink_to_fit(),它请求向量删除未使用的容量.

                Note: Since this old answer is still getting upvotes (thus people read it), I feel the need to add that C++11 has added std::vector<...>::shrink_to_fit(), which requests the vector to remove unused capacity.

                这篇关于调用 std::vector::clear() 会将 std::vector::capacity() 设置为零吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:在 std::vector 上的 Openmp 和减少? 下一篇:C++中的向量交集

                相关文章

                最新文章

                  • <bdo id='5emBp'></bdo><ul id='5emBp'></ul>
                  <legend id='5emBp'><style id='5emBp'><dir id='5emBp'><q id='5emBp'></q></dir></style></legend>
                1. <small id='5emBp'></small><noframes id='5emBp'>

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