如果向量有足够的空间(通过保留创建),std :: vector :: insert()无效迭代器? [英] Does std::vector::insert() invalidate iterators if the vector has enough room (created through reserve)?

查看:121
本文介绍了如果向量有足够的空间(通过保留创建),std :: vector :: insert()无效迭代器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

回答如何自我复制载体?让我有点混淆关于迭代器无效。一些文献说如果你使用插入,push_back等,考虑所有迭代器无效。这很清楚,它可能导致矢量增长,这使迭代器无效。在特殊情况下,我知道将有足够的空间?



首先尝试:

  myvec.reserve(myvec.size()* 3); //这保护我从迭代器无效? 
vector< string> :: iterator it = myvec.end();
myvec.insert(myvec.end(),myvec.begin(),it);
myvec.insert(myvec.end(),myvec.begin(),it);

经过一些优秀的答案后,第二次尝试:

  auto size = myvec.size(); 
myvec.reserve(size * 3); //这保护我从迭代器无效?
myvec.insert(myvec.end(),myvec.begin(),myvec.begin()+ size);
myvec.insert(myvec.end(),myvec.begin(),myvec.begin()+ size);

第三次尝试后:



<$> p $ p> auto size = myvec.size();
myvec.reserve(size * 3); //这保护我从迭代器无效?
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 ++标准库参考:


插入或删除元素
使引用,指针和
无效的迭代器引用以下
元素。如果插入导致
重新分配,它会使所有
引用,迭代器和指针无效。


代码是安全和定义的行为。在标准中是否有保证这一点的段落?

解决方案

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


如果没有重新分配,插入点之前的所有迭代器和引用都保持有效。 / p>

这里的关键是插入点之前。由于您的原始 it 不在插入点之前(因为它是插入点),我将不会对其保留有效。 / p>

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?

first try:

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);

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?

解决方案

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.

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()无效迭代器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆