KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I have a list iterator that goes through a list and removes all the even numbers. I can use the list iterator to print out the numbers fine but I cannot use the list's remove() and pass in the dereferenced iterator. I noticed that when the remove() statement is in effect, *itr gets corrupted? Can somebody explain this? #include <iostream> #include <list> #define MAX 100 using namespace std; int main() { list<int> listA; list<int>::iterator itr; //create list of 0 to 100 for(int i=0; i<=MAX; i++) listA.push_back(i); //remove even numbers for(itr = listA.begin(); itr != listA.end(); ++itr) { if ( *itr % 2 == 0 ) { cout << *itr << endl; listA.remove(*itr); //comment this line out and it will print properly } } }
Tags (comma-separated)
Save Edits
Cancel