• <small id='Wo4NT'></small><noframes id='Wo4NT'>

    <legend id='Wo4NT'><style id='Wo4NT'><dir id='Wo4NT'><q id='Wo4NT'></q></dir></style></legend>

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

        <bdo id='Wo4NT'></bdo><ul id='Wo4NT'></ul>
      1. std::vector 向下调整大小

        时间:2023-09-15

      2. <tfoot id='0abUN'></tfoot>

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

        <small id='0abUN'></small><noframes id='0abUN'>

            • <bdo id='0abUN'></bdo><ul id='0abUN'></ul>

                  <tbody id='0abUN'></tbody>

                  <legend id='0abUN'><style id='0abUN'><dir id='0abUN'><q id='0abUN'></q></dir></style></legend>
                  本文介绍了std::vector 向下调整大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  C++ 标准似乎没有声明任何一方对容量的副作用resize(n),用 n clear().

                  The C++ standard seems to make no statement regarding side-effects on capacity by either resize(n), with n < size(), or clear().

                  它确实声明了 push_backpop_back - O(1)

                  It does make a statement about amortized cost of push_back and pop_back - O(1)

                  我可以设想一个执行通常类型的容量更改的实现ala CLRS 算法(例如,放大时加倍,将 size 减小到 < capacity()/4 时减半).(Cormen Lieserson Rivest Stein)

                  I can envision an implementation that does the usual sort of capacity changes ala CLRS Algorithms (e.g. double when enlarging, halve when decreasing size to < capacity()/4). (Cormen Lieserson Rivest Stein)

                  有人有任何实施限制的参考吗?

                  Does anyone have a reference for any implementation restrictions?

                  推荐答案

                  以较小的尺寸调用 resize()vector 的容量没有影响.它不会释放内存.

                  Calling resize() with a smaller size has no effect on the capacity of a vector. It will not free memory.

                  vector 释放内存的标准习惯用法是用一个空的临时 vector swap() 它: std::vector().swap(vec);.如果要向下调整大小,则需要从原始向量复制到新的局部临时向量,然后将结果向量与原始向量交换.

                  The standard idiom for freeing memory from a vector is to swap() it with an empty temporary vector: std::vector<T>().swap(vec);. If you want to resize downwards you'd need to copy from your original vector into a new local temporary vector and then swap the resulting vector with your original.

                  更新: C++11 添加了一个成员函数 shr​​ink_to_fit() 出于此目的,这是一个将 capacity() 减少到 size() 的非绑定请求.

                  Updated: C++11 added a member function shrink_to_fit() for this purpose, it's a non-binding request to reduce capacity() to size().

                  这篇关于std::vector 向下调整大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

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

                    <legend id='bqquG'><style id='bqquG'><dir id='bqquG'><q id='bqquG'></q></dir></style></legend>

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

                        <tbody id='bqquG'></tbody>
                        <bdo id='bqquG'></bdo><ul id='bqquG'></ul>

                          <tfoot id='bqquG'></tfoot>