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

    <tfoot id='DTF8I'></tfoot>

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

        调整向量的大小是否会使迭代器失效?

        时间:2023-09-15

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

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

                <tbody id='gh8Fa'></tbody>
                  <legend id='gh8Fa'><style id='gh8Fa'><dir id='gh8Fa'><q id='gh8Fa'></q></dir></style></legend><tfoot id='gh8Fa'></tfoot>
                  本文介绍了调整向量的大小是否会使迭代器失效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我发现这个 C++ 代码:

                  I found that this C++ code:

                  vector<int> a;
                  a.push_back(1);
                  a.push_back(2);
                  vector<int>::iterator it = a.begin();
                  a.push_back(4);
                  cout << *it;
                  

                  打印一些大的随机数;但是如果你在第 3 和第 4 行之间添加 a.push_back(3),它会打印 1.你能给我解释一下吗?

                  print some big random number; but if you add a.push_back(3) between 3rd and 4th lines, it will print 1. Can you explain it to me?

                  推荐答案

                  措辞更谨慎

                  是的,调整向量的大小可能会使指向该向量的所有迭代器无效.

                  yes, resizing a vector might invalidate all iterators pointing into the vector.

                  vector 是通过内部分配存储数据的数组来实现的.当向量增长时,该数组可能会耗尽空间,当它耗尽时,向量会分配一个新的更大的数组,将数据复制到该数组,然后删除旧数组.

                  The vector is implemented by internally allocating an array where the data is stored. When the vector grows, that array might run out of space, and when it does, the vector allocates a new, bigger, array, copies the data over to that, and then deletes the old array.

                  因此,指向旧内存的旧迭代器不再有效.但是,如果矢量被向下调整大小(例如通过pop_back()),则使用相同的数组.数组永远不会自动缩小.

                  So your old iterators, which point into the old memory, are no longer valid. If the vector is resized downwards (for example by pop_back()), however, the same array is used. The array is never downsized automatically.

                  避免这种重新分配(和指针失效)的一种方法是首先调用 vector::reserve(),以留出足够的空间,以便不需要进行这种复制.在您的情况下,如果您在第一个 push_back() 操作之前调用了 a.reserve(3),那么内部数组将足够大,以至于 push_back 的执行无需重新分配数组,因此您的迭代器将保持有效.

                  One way to avoid this reallocation (and pointer invalidation) is to call vector::reserve() first, to set aside enough space that this copying isn't necessary. In your case, if you called a.reserve(3) before the first push_back() operation, then the internal array would be big enough that the push_back's can be performed without having to reallocate the array, and so your iterators will stay valid.

                  这篇关于调整向量的大小是否会使迭代器失效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:为什么 emplace_back() 不使用统一初始化? 下一篇:std::vectors 的 std::vectors 连续性

                  相关文章

                  最新文章

                  • <bdo id='5LXKX'></bdo><ul id='5LXKX'></ul>

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

                      <tfoot id='5LXKX'></tfoot>

                      <legend id='5LXKX'><style id='5LXKX'><dir id='5LXKX'><q id='5LXKX'></q></dir></style></legend>

                      <small id='5LXKX'></small><noframes id='5LXKX'>