Reputation: 1
Even though I looked at that page: http://www.cplusplus.com/reference/vector/vector/erase/ , I still don't understand why I am having a runtime error on VS2010 ('vector iterator not incrementable') when executing the last line of the following code:
vector<int> vec;
for(int i = 0 ; i < 10 ; i++)
vec.push_back(i);
auto itb = vec.begin()+1; // 2nd item of the vector
auto it = itb + 2; // 4th item of the vector
it = vec.erase(itb, it); // remove the 2nd & 3rd
++it; // Error happens when trying to execute this line
I thought erase would return an iterator on the item that is just after the last removed one. So, here it would be pointing on item with value 3. Here, as the vector is long enough erase doesn't return vec.end(). From there, I should be able to iterate with a valid iterator. But no! Why?
Whereas that code works:
vector<int> vec;
for(int i = 0 ; i < 10 ; i++)
vec.push_back(i);
auto itb = vec.begin()+1;
auto it = itb + 2;
vec.erase(itb, it);
it = vec.begin()+1; // re-generate an iterator from the begin() one.
++it;
Edit: If I slightly modify the code from "Vlad from Moscow" (see below) in an empty main.cpp file, then I am still having the problem. So, I guess I have a compiler issue.
Here is the code:
#include <iostream>
#include <vector>
int main()
{
std::vector<int> v;
for ( int i = 0; i < 10; i++ ) v.push_back( i );
for ( auto it = v.begin(); it != v.end() ; ++it ) std::cout << *it << ' ';
std::cout << std::endl;
auto itb = v.begin() + 1;
auto it = itb + 2;
it = v.erase( itb, it );
++it; // Crash here
for ( auto it = v.begin(); it != v.end() ; ++it ) std::cout << *it << ' ';
std::cout << std::endl;
while ( it != v.end() ) std::cout << *it++ << ' ';
std::cout << std::endl;
return 0;
}
Let me describe my config: I am running a Win7 Ultimate 64bit under Parallel desktop (Mac), with VC++6, VCS2003, VS2005, VS2008, VS2010 (no SP, .Net framework 4.5) installed.
Upvotes: 0
Views: 236
Reputation: 310950
This example demonstrates that there is nothing wrong in your first code snippet
#include <iostream>
#include <vector>
int main()
{
std::vector<int> v;
for ( int i = 0; i < 10; i++ ) v.push_back( i );
for ( int x : v ) std::cout << x << ' ';
std::cout << std::endl;
auto itb = v.begin() + 1;
auto it = itb + 2;
it = v.erase( itb, it );
++it;
for ( int x : v ) std::cout << x << ' ';
std::cout << std::endl;
while ( it != v.end() ) std::cout << *it++ << ' ';
std::cout << std::endl;
return 0;
}
The output is
0 1 2 3 4 5 6 7 8 9
0 3 4 5 6 7 8 9
4 5 6 7 8 9
If to remove statement
++it;
then the output will be
0 1 2 3 4 5 6 7 8 9
0 3 4 5 6 7 8 9
3 4 5 6 7 8 9
EDIT: Here is the same program running in MS VC++ 2010
#include "stdafx.h"
#include <iostream>
#include <vector>
int _tmain(int argc, _TCHAR* argv[])
{
std::vector<int> v;
for ( int i = 0; i < 10; i++ ) v.push_back( i );
for each ( int x in v ) std::cout << x << ' ';
std::cout << std::endl;
auto itb = v.begin() + 1;
auto it = itb + 2;
it = v.erase( itb, it );
++it;
for each ( int x in v ) std::cout << x << ' ';
std::cout << std::endl;
while ( it != v.end() ) std::cout << *it++ << ' ';
std::cout << std::endl;
}
I hope these examples will help you to find your real error.
Upvotes: 1