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

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

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

        <tfoot id='nOjHr'></tfoot>
      1. 是否在清除后释放 std::vector 内存?

        时间:2023-09-16

      2. <small id='x6fwj'></small><noframes id='x6fwj'>

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

                  <tbody id='x6fwj'></tbody>
                • <bdo id='x6fwj'></bdo><ul id='x6fwj'></ul>
                • <legend id='x6fwj'><style id='x6fwj'><dir id='x6fwj'><q id='x6fwj'></q></dir></style></legend>
                  本文介绍了是否在清除后释放 std::vector 内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  假设我有一个 std::vector 结构体.如果向量被 clear() 处理,内存会发生什么变化?

                  Suppose I have a std::vector of structs. What happens to the memory if the vector is clear()'d?

                  std::vector<myStruct> vecs;
                  vecs.resize(10000);
                  vecs.clear();
                  

                  内存会被释放,还是作为可重用的缓冲区附加到 vecs 变量上?

                  Will the memory be freed, or still attached to the vecs variable as a reusable buffer?

                  推荐答案

                  内存仍然附着在向量上.这也不仅仅是可能的.这是必需的.特别是,如果您要在调用 clear() 后再次向向量添加元素,则在您添加的元素数量超过之前设置的 1000 个元素之前,向量不得重新分配.

                  The memory remains attached to the vector. That isn't just likely either. It's required. In particular, if you were to add elements to the vector again after calling clear(), the vector must not reallocate until you add more elements than the 1000 is it was previously sized to.

                  如果你想释放内存,通常是用一个空向量交换.C++11 还添加了一个 shr​​ink_to_fit 旨在更直接地提供大致相同功能的成员函数,但它是非绑定的(换句话说,它可能会释放额外的内存,但仍然不是真正需要这样做).

                  If you want to free the memory, the usual is to swap with an empty vector. C++11 also adds a shrink_to_fit member function that's intended to provide roughly the same capability more directly, but it's non-binding (in other words, it's likely to release extra memory, but still not truly required to do so).

                  这篇关于是否在清除后释放 std::vector 内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:'volatile' 在 C++ 中阻止了哪些优化? 下一篇:编译器什么时候内联 C++ 代码?

                  相关文章

                  最新文章

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

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

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

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