fungus
11/1/2008 11:02:00 AM
On Nov 1, 11:52 am, Angus <anguscom...@gmail.com> wrote:
>
> I get an access violation in the loop iteration after an erase.
Yep, you just freed the memory referenced by "it".
> What
> is the recommended way to deal with this? ie iterate through a
> container removing elements which meet a criterion? remove?
>
std::list<int>::iterator it = mylist.begin();
while (it != mylist.end()) {
if(*it == 3) {
std::cout << "deleting 3" << std::endl;
it = mylist.erase(it);
}
else {
++it;
}
}