• <small id='6bc3l'></small><noframes id='6bc3l'>

        • <bdo id='6bc3l'></bdo><ul id='6bc3l'></ul>
      1. <i id='6bc3l'><tr id='6bc3l'><dt id='6bc3l'><q id='6bc3l'><span id='6bc3l'><b id='6bc3l'><form id='6bc3l'><ins id='6bc3l'></ins><ul id='6bc3l'></ul><sub id='6bc3l'></sub></form><legend id='6bc3l'></legend><bdo id='6bc3l'><pre id='6bc3l'><center id='6bc3l'></center></pre></bdo></b><th id='6bc3l'></th></span></q></dt></tr></i><div id='6bc3l'><tfoot id='6bc3l'></tfoot><dl id='6bc3l'><fieldset id='6bc3l'></fieldset></dl></div>
      2. <tfoot id='6bc3l'></tfoot>
        <legend id='6bc3l'><style id='6bc3l'><dir id='6bc3l'><q id='6bc3l'></q></dir></style></legend>

        在向量中使用 Reserve() 的好处 - C++

        时间:2023-10-07

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

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

                  <tbody id='HSzBW'></tbody>
                  本文介绍了在向量中使用 Reserve() 的好处 - C++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  在处理向量时使用 Reserve 有什么好处.我应该什么时候使用它们?无法找到明确的答案,但我认为在使用它们之前提前预订会更快.

                  What is the benefit of using reserve when dealing with vectors. When should I use them? Couldn't find a clear cut answer on this but I assume it is faster when you reserve in advance before using them.

                  怎么说你们比我聪明?

                  推荐答案

                  如果您知道向量最终将包含多少个元素,这很有用 - 它可以帮助向量避免重复分配内存(并且必须将数据移动到新的记忆).

                  It's useful if you have an idea how many elements the vector will ultimately hold - it can help the vector avoid repeatedly allocating memory (and having to move the data to the new memory).

                  一般来说,这可能是您不需要担心的潜在优化,但它也无害(最坏的情况是,如果您高估,最终会浪费内存).

                  In general it's probably a potential optimization that you shouldn't need to worry about, but it's not harmful either (at worst you end up wasting memory if you over estimate).

                  当您希望确保现有迭代器不会因添加新元素而失效时,它可以不仅仅是优化的一个领域.

                  One area where it can be more than an optimization is when you want to ensure that existing iterators do not get invalidated by adding new elements.

                  例如,push_back() 调用可能会使向量的现有迭代器无效(如果发生重新分配).但是,如果您保留了足够多的元素,则可以确保不会发生重新分配.不过,这是一种不需要经常使用的技术.

                  For example, a push_back() call may invalidate existing iterators to the vector (if a reallocation occurs). However if you've reserved enough elements you can ensure that the reallocation will not occur. This is a technique that doesn't need to be used very often though.

                  这篇关于在向量中使用 Reserve() 的好处 - C++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:遍历指针向量 下一篇:将字符串解析为 int 向量

                  相关文章

                  最新文章

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

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

                  2. <legend id='DSPht'><style id='DSPht'><dir id='DSPht'><q id='DSPht'></q></dir></style></legend>