C + + STL list traversal Delete crash error usage one
The following usage will crash in for, analyze the first for loop it=0, and it becomes 17891602 when t.erase (it) executes.
Indicates that it can no longer operate as an iterator, and will naturally error.
#include <map> #include <list>using namespace std;typedef std::list<int > Testlist;int _tmain (int argc , _tchar* argv[]) {testlist t;for (int i = 0; i < 10;i++) {t.push_back (i);} for (Testlist::iterator it = T.begin (); It! = T.end ();) {t.erase (it); it++;} return 0;}
Error usage Two
The following error is the same as the error in this usage
#include <map> #include <list>using namespace std;typedef std::list<int > Testlist;int _tmain (int argc , _tchar* argv[]) {testlist t;for (int i = 0; i < 10;i++) {t.push_back (i);} for (Testlist::iterator it = T.begin (); It! = T.end (); it++) {t.erase (it);} return 0;}
Error usage Three
The following kind of usage think that it++ will not be a problem, in fact, their mistakes are the same, that is t.erase (it) after it has been a non-iterative, naturally not as an iterative operation
#include "stdafx.h" #include <map> #include <list>using namespace std;typedef std::list<int > Testlist ; int _tmain (int argc, _tchar* argv[]) {testlist t;for (int i = 0; i < 10;i++) {t.push_back (i);} for (Testlist::iterator it = T.begin (); It! = T.end ();) {t.erase (it);} return 0;}
Correct usage
#include <map> #include <list>using namespace std;typedef std::list<int > Testlist;int _tmain (int argc , _tchar* argv[]) {testlist t;for (int i = 0; i < 10;i++) {t.push_back (i);} for (Testlist::iterator it = T.begin (); It! = T.end ();) {t.erase (it++);} return 0;}
C + + STL list Traversal Delete problem