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

<tfoot id='swWax'></tfoot>
    1. <small id='swWax'></small><noframes id='swWax'>

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

        std::vector.pop_back() 会改变向量的容量吗?

        时间:2023-10-07
          <tbody id='ptUK6'></tbody>

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

              <bdo id='ptUK6'></bdo><ul id='ptUK6'></ul>

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

              • <tfoot id='ptUK6'></tfoot>

                  本文介绍了std::vector.pop_back() 会改变向量的容量吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  如果我在程序开始时使用 resize()reserve() 将 std::vector 分配到特定大小和容量,是否有可能pop_back() 可能会破坏"保留容量并导致重新分配?

                  If I allocated an std::vector to a certain size and capacity using resize() and reserve() at the beginning of my program, is it possible that pop_back() may "break" the reserved capacity and cause reallocations?

                  推荐答案

                  没有.缩小向量容量的唯一方法是交换技巧

                  No. The only way to shrink a vector's capacity is the swap trick

                  template< typename T, class Allocator >
                  void shrink_capacity(std::vector<T,Allocator>& v)
                  {
                     std::vector<T,Allocator>(v.begin(),v.end()).swap(v);
                  }
                  

                  即使这样也不能保证按照标准工作.(虽然很难想象它不会工作的实现.)

                  and even that isn't guaranteed to work according to the standard. (Although it's hard to imagine an implementation where it wouldn't work.)

                  据我所知,C++ 标准的下一个版本(以前是 C++0x,但现在变成了 C++1x)将具有 std::vector<>::shrink_to_fit().

                  As far as I know, the next version of the C++ standard (what used to be C++0x, but now became C++1x) will have std::vector<>::shrink_to_fit().

                  这篇关于std::vector.pop_back() 会改变向量的容量吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:C++ 向量::清除 下一篇:如何在循环中向空向量添加元素?

                  相关文章

                  最新文章

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

                  2. <tfoot id='XRdk1'></tfoot>

                      <legend id='XRdk1'><style id='XRdk1'><dir id='XRdk1'><q id='XRdk1'></q></dir></style></legend>
                        <bdo id='XRdk1'></bdo><ul id='XRdk1'></ul>