Assuming you want to remove any occurrence of 5 from the list, you should either use the "classical" erase-while-iterating pattern,
or use the list's member function:Code:for (iter = myList.begin() ; iter != myList.end() ; ) { std::cout << (*iter) << std::endl; if ((*iter) == 5) iter = myList.erase(iter); else ++iter; }
Code:myList.remove(5);