A
Angus
If I want to erase all list items with a value of say 3 as below:
std::list<int> mylist;
mylist.push_back(3);
mylist.push_back(4);
mylist.push_back(5);
mylist.push_back(6);
for(std::list<int>::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<int> mylist;
mylist.push_back(3);
mylist.push_back(4);
mylist.push_back(5);
mylist.push_back(6);
for(std::list<int>::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