Strange iterator behaviour
Posted
by A-ha
on Stack Overflow
See other posts from Stack Overflow
or by A-ha
Published on 2010-06-16T10:07:19Z
Indexed on
2010/06/16
21:32 UTC
Read the original article
Hit count: 114
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
string s = "Haven't got an idea why.";
auto beg = s.begin();
auto end = s.end();
while (beg < end)
{
cout << *beg << '\n';
if (*beg == 'a')
{//whithout if construct it works perfectly
beg = s.erase(beg);
}
++beg;
}
return 0;
}
Why if I erase one or more chars from this string this code breaks? I suppose it has something to do with returned iterator after erase operation being created at higher address than end iterator but I'm not sure and it surely isn't right behaviour. Or is it?
© Stack Overflow or respective owner