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

<tfoot id='yqD5X'></tfoot>
    <bdo id='yqD5X'></bdo><ul id='yqD5X'></ul>

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

      如果向量有足够的空间(通过保留创建),std::vec

      时间:2023-09-15
    1. <small id='MKE9L'></small><noframes id='MKE9L'>

          1. <tfoot id='MKE9L'></tfoot>

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

                <tbody id='MKE9L'></tbody>
            1. <legend id='MKE9L'><style id='MKE9L'><dir id='MKE9L'><q id='MKE9L'></q></dir></style></legend>
                <bdo id='MKE9L'></bdo><ul id='MKE9L'></ul>
                本文介绍了如果向量有足够的空间(通过保留创建),std::vector::insert() 是否会使迭代器失效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                回答如何自我复制向量?让我明白对迭代器失效有点困惑.一些文献说如果您使用 insert、push_back 等,请考虑所有迭代器无效".很明显,它可能会导致向量增长,从而使迭代器无效.如果我知道有足够的空间,那么特殊情况呢?

                Answering How to self-copy a vector? has got me a bit confused about iterator invalidation. Some literature says "if you use insert, push_back, etc. consider all iterators invalid". Thats clear, it might cause the vector to grow which invalidates iterators. What about the special case where I know there is going to be enough room?

                第一次尝试:

                myvec.reserve(myvec.size()*3);  //does this protect me from iterator invalidation?
                vector<string>::iterator it = myvec.end();    
                myvec.insert(myvec.end(), myvec.begin(), it);
                myvec.insert(myvec.end(), myvec.begin(), it);
                

                经过一些出色的回答后,第二次尝试:

                After some excellent answers second try:

                auto size = myvec.size();
                myvec.reserve(size*3);  //does this protect me from iterator invalidation?  
                myvec.insert(myvec.end(), myvec.begin(), myvec.begin()+size);
                myvec.insert(myvec.end(), myvec.begin(), myvec.begin()+size);
                

                在获得更多优秀答案后第三次尝试:

                After more excellent answers third try:

                auto size = myvec.size();
                myvec.reserve(size*3);  //does this protect me from iterator invalidation?  
                back_insert_iterator< vector<string> > back_it (myvec);
                copy (myvec.begin(),myvec.begin()+size,back_it);
                copy (myvec.begin(),myvec.begin()+size,back_it);
                

                此引自 Josuttis 的C++ 标准库参考":

                This quote from Josuttis' "C++ Standard Library Reference":

                插入或删除元素使引用、指针和引用以下内容的迭代器元素.如果插入导致重新分配,它使所有引用、迭代器和指针.

                Inserting or removing elements invalidates references, pointers, and iterators that refer to the following element. If an insertion causes reallocation, it invalidates all references, iterators, and pointers.

                表明我的代码是安全的和定义的行为.标准中有没有保证这一点的段落?

                suggests that my code is safe and defined behavior. Is there a passage in the standard which guaranties this?

                推荐答案

                过去的迭代器总是有点特别.我会小心的.标准是这样说的(23.3.6.5):

                The past-the-end iterator is always a bit special. I'd be careful. The standard says this (23.3.6.5):

                如果没有发生重新分配,插入点之前的所有迭代器和引用都保持有效.

                If no reallocation happens, all the iterators and references before the insertion point remain valid.

                这里的关键是插入点之前".由于您的原始 it 不在插入点之前(因为它 插入点),我不会相信它仍然有效.

                The key here is "before the insertion point". Since your original it is not before the insertion point (since it is the insertion point), I wouldn't bank on it remaining valid.

                这篇关于如果向量有足够的空间(通过保留创建),std::vector::insert() 是否会使迭代器失效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:在二进制文件 C++ 中写入和加载结构向量 下一篇:C++ STL 向量:从索引获取迭代器?

                相关文章

                最新文章

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

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

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

                  2. <tfoot id='E0Gj8'></tfoot>