向量的a.insert()之后的迭代器增量的泛行为... [英] Wild behaviour of incrementation of iterator after a.insert() for vector...

查看:67
本文介绍了向量的a.insert()之后的迭代器增量的泛行为...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是我的代码..

我通过评论评论了代码中的问题。请检查一下。

谢谢。

here goes my code..
i have commented the problems in the code by comment . please do check it.
thanks.

vector<int>a = { 4,5,2,9,6 };
	vector<int>::iterator iter = find(a.begin(), a.end(), 2);
	a.insert(iter, 8);				//what does iter point after doing this insertion ?
	iter++;					// it is not allowing this.
	a.erase(iter);			
	auto itr = a.begin();
	while (itr != a.end())
	{
		cout << *itr << endl;
		itr++;
	}
	return 0;





我的尝试:



i没有尝试过什么特别的东西。我的意思是它完美地为列表...它甚至没有为deque工作..



那么为什么它会发生deque和vector?



What I have tried:

i have tried nothing special. i mean it worked out perfectly for list.. and it didnt even worked for the deque..

so why is it happening with deque and vector?

推荐答案

标准指示迭代器在哪种情况下失效。在使用STL容器并记住规则之前,您需要阅读好的文档。一旦你知道规则,你应该能够知道热写正确的代码。



简化的规则是插入/删除vector / deque的无效迭代器。 br />


忽略错误(缺少元素,空列表......),以下代码应该有效:

The standard indicate in which situation an iterator is invalidated. You need to read good documentation preferably before using STL containers and remember the rules. Once you know the rules, you should be able to know hot to write correct code.

Simplified rules would be that insert/remove invalidate iterators for vector/deque.

Ignoring errors (missing elements, empty list...), following code should works:
auto iter = find(a.begin(), a.end(), 2);

// Since erase invalidate iterators from the erased position, 
// we could make a copy and increment it so that original 
// iterator won't be affected by deletion (since it is before)
// The after that, we can insert the item.
auto it_erase = iter;
++it_erase;

a.erase(it_erase);
a.insert(iter, 8);     
	
for(itr = a.begin(); itr != a.end(); ++itr)
{
    cout << *itr << endl;
}

return 0;





顺便说一下,查找信息并不困难使用Google: vector :: erase - C ++ Reference [ ^ ]



如果删除必须在插入后完成(取决于在插入项时抛出异常时代码的行为方式), insert 将返回位于第一个插入元素的迭代器。



因此这样的事情应该做:



By the way, it is not hard to find information using Google: vector::erase - C++ Reference[^]

If deletion must be done after insertion (depending on how you want code to behave if an exception is thrown while inserting an item), insert will returns an iterator positioned at the first inserted element.

Thus something like this should do:

auto it_erase = a.insert(iter, 8) + 1; // Or maybe +2 
a.erase(it_erase);





我不确定它是否应为+1或+2,因为您的代码无效且预期结果未在问题中显示...



如果你使用+1,你会得到:



I am not sure if it should be +1 or +2 as your code being invalid and expected result not indicated in the question...

If you use +1, you would get:

4 5 8 9 6





如果你使用+2,你会得到:



If you use +2, you would get:

4 5 8 2 6





如果现有代码具有正确的行为,那么 +1 将是合适的。如果带有list的相同代码给出了所需的结果,则应使用+2。这个问题不明确......



顺便说一句,如果你使用Visual Studio并编译 DEBUG 版本,它可能告诉你你正在使用一个无效的迭代器。



规则定义至少20年以前,因为我有一本1996年的书这记录在其中。



If existing code has proper behavior, then +1 would be appropriate. If the same code with list give the desired result, then +2 should be used. This is not clear from the question...

By the way, if you are using Visual Studio and compiling the DEBUG version, it probably tells you that you are using an invalidated iterator.

Rules are well defined since at least 20 years ago as I have one book from 1996 and this is documented in it.


这篇关于向量的a.insert()之后的迭代器增量的泛行为...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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