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

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

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

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

        在 vector::push_back 内存明智的引擎盖下会发生什么

        时间:2023-10-07

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

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

          <bdo id='c5TSq'></bdo><ul id='c5TSq'></ul>
          • <tfoot id='c5TSq'></tfoot>
            <i id='c5TSq'><tr id='c5TSq'><dt id='c5TSq'><q id='c5TSq'><span id='c5TSq'><b id='c5TSq'><form id='c5TSq'><ins id='c5TSq'></ins><ul id='c5TSq'></ul><sub id='c5TSq'></sub></form><legend id='c5TSq'></legend><bdo id='c5TSq'><pre id='c5TSq'><center id='c5TSq'></center></pre></bdo></b><th id='c5TSq'></th></span></q></dt></tr></i><div id='c5TSq'><tfoot id='c5TSq'></tfoot><dl id='c5TSq'><fieldset id='c5TSq'></fieldset></dl></div>
                  <tbody id='c5TSq'></tbody>
                  本文介绍了在 vector::push_back 内存明智的引擎盖下会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我的问题是关于 vector::push_back 的效果,我知道它在矢量的末尾添加了一个元素,但在引擎盖下会发生什么?

                  My question is regarding the effect of vector::push_back, I know it adds an element in the end of the vector but what happens underneath the hood?

                  IIRC 内存对象是按顺序分配的,所以我的问题是 vector::push_back 是否只是在向量之后立即分配更多内存,如果是这样,如果没有足够的空闲内存会发生什么在那个位置?或者也许在结束"中添加了一个指针以使向量跳"到它继续的位置?或者它只是通过将其复制到另一个有足够空间的位置而重新分配,旧副本被丢弃?或者别的什么?

                  IIRC memory objects are allocated in a sequential manner, so my question is whether vector::push_back simply allocates more memory immediately after the vector, and if so what happens if there is not enough free memory in that location? Or perhaps a pointer is added in the "end" to cause the vector to "hop" to the location it continues? Or is it simply reallocated through copying it to another location that has enough space and the old copy gets discarded? Or maybe something else?

                  推荐答案

                  如果已经分配了足够的空间,则对象将根据原位参数复制构造.当没有足够的内存时,向量将按照某种几何级数增长它的内部数据缓冲区(每次新大小将是 k*old_sizek > 1[1]) 并且原始缓冲区中存在的所有对象将移动到新缓冲区.操作完成后,旧缓冲区将被释放到系统中.

                  If there is enough space already allocated, the object is copy constructed from the argument in place. When there is not enough memory, the vector will grow it's internal databuffer following some kind of geometric progression (each time the new size will be k*old_size with k > 1[1]) and all objects present in the original buffer will then be moved to the new buffer. After the operation completes the old buffer will be released to the system.

                  在前面的句子中move不是在技术move-constructor/move-assignment意义上使用的,它们可以是移动复制或任何等效操作.

                  In the previous sentence move is not used in the technical move-constructor/ move-assignment sense, they could be moved or copied or any equivalent operation.

                  [1] 按因子 k > 增长1 确保 push_back 的摊销成本是恒定的.实际常量因一种实现而异(Dinkumware 使用 1.5,gcc 使用 2).摊销成本意味着即使每隔一段时间 push_back 会非常昂贵(O(N) 当时的向量大小),这些情况也很少发生足以使整个插入集的所有操作的成本与插入次数成线性关系,因此每次插入平均一个恒定成本)

                  [1] Growing by a factor k > 1 ensures that the amortized cost of push_back is constant. The actual constant varies from one implementation to another (Dinkumware uses 1.5, gcc uses 2). The amortized cost means that even if every so often one push_back will be highly expensive (O(N) on the size of the vector at the time), those cases happen rarely enough that the cost of all operations over the whole set of insertions is linear in the number of insertions, and thus each insertion averages a constant cost)

                  这篇关于在 vector::push_back 内存明智的引擎盖下会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:指针的排序向量 下一篇:获取 STL 向量中大于某个值的元素的所有位置

                  相关文章

                  最新文章

                      <bdo id='07Vnj'></bdo><ul id='07Vnj'></ul>

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

                      <legend id='07Vnj'><style id='07Vnj'><dir id='07Vnj'><q id='07Vnj'></q></dir></style></legend>