If I want to erase all list items with a value of say 3 as below:
std::list<intmy list;
mylist.push_bac k(3);
mylist.push_bac k(4);
mylist.push_bac k(5);
mylist.push_bac k(6);
for(std::list<i nt>::iterator it = mylist.begin(); it !=
mylist.end(); ++it) {
if(*it == 3) {
std::cout << "deleting 3" << std::endl;
mylist.erase(it );
}
}
I get an access violation in the loop iteration after an erase. What
is the recommended way to deal with this? ie iterate through a
container removing elements which meet a criterion? remove?
A
std::list<intmy list;
mylist.push_bac k(3);
mylist.push_bac k(4);
mylist.push_bac k(5);
mylist.push_bac k(6);
for(std::list<i nt>::iterator it = mylist.begin(); it !=
mylist.end(); ++it) {
if(*it == 3) {
std::cout << "deleting 3" << std::endl;
mylist.erase(it );
}
}
I get an access violation in the loop iteration after an erase. What
is the recommended way to deal with this? ie iterate through a
container removing elements which meet a criterion? remove?
A
Comment