Click here to Skip to main content
15,892,269 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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;


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?
Posted
Updated 6-Jun-16 13:04pm
v3
Comments
Philippe Mori 6-Jun-16 19:06pm    
You don't tell what you expect to get as an answer...
Philippe Mori 6-Jun-16 19:09pm    
By the way, most expert C++ programmer would use ++iter instead of iter++ as the first one might be more efficient (no temporaries).

Also for loop would be use since it exactly match for loop pattern.

See my solution for those improvements.

1 solution

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;


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:
C++
auto it_erase = a.insert(iter, 8) + 1; // Or maybe +2 
a.erase(it_erase);


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


If you use +2, you would get:
4 5 8 2 6


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.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900