Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iterator erase (iterator first, iterator last) doesn't work under Visual C++ 2010 Express

Tags:

c++

stl

vector

I've found a problem while writing code under Visual C++ 2010 Express. When executing last line I get a runtime error "vector iterator not dereferencable". What is wrong with the code below?

vector<int> vec (5, 1001);

vector<int>::iterator begin = vec.begin();
vector<int>::iterator end = vec.begin();

begin++; //std::advance(begin, 1); gives the same result
end++; end++; end++; //std::advance(end, 3); gives the same result

cout << (*begin) << endl;
cout << (*end) << endl;
begin = vec.erase(begin, end);
cout << (*begin) << endl;              //It doesn't work

This code works under gcc. When elements are erased one by one it works in VC++ 2010 Express too.

Is it a bug in VC++ 2010 Express?

like image 294
ThomasN Avatar asked May 13 '26 21:05

ThomasN


1 Answers

If your not using SP1 then this VC10 bug might have hit you, there is also some kind of workaround listed there.

like image 54
Surt Avatar answered May 15 '26 11:05

Surt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!