1#include <iostream>2#include <vector>3#include <string>4 5 using namespacestd;6 7 intMainvoid)8 {9vector<string>Vecstr;TenVecstr.push_back ("AAA"); OneVecstr.push_back ("BBB"); AVecstr.push_back ("CCC"); -Vecstr.push_back ("DDD"); -Vecstr.push_back ("Eee"); theVecstr.push_back ("FFF"); -Vecstr.push_back ("GGG"); - - for(vector<string>::iterator iter = Vecstr.begin (); Iter! = Vecstr.end (); ++iter) + { -cout << (*iter) <<Endl; + } Acout <<"----------------------"<<Endl; at - - for(vector<string>::iterator iter = Vecstr.begin (); Iter! =vecstr.end ();) - { -cout <<"Execute Item:"<< (*iter) <<Endl; - if("GGG"== (*iter)) in { -cout << iter._ptr <<" "<< * (iter._ptr) <<Endl; to //Recommended Way + iter = vecstr.erase (ITER); -cout << iter._ptr <<" "<< * (iter._ptr) <<Endl; the } * Else $ {Panax Notoginseng++iter; - } the } + Acout <<"----------------------"<<Endl; the for(vector<string>::iterator iter = Vecstr.begin (); Iter! = Vecstr.end (); ++iter) + { -cout << (*iter) <<Endl; $ } $ -}
All projects
1 for(vector<string>::iterator iter = Vecstr.begin (); Iter! =vecstr.end ();)2 {3cout <<"Execute Item:"<< (*iter) <<Endl; 4cout << iter._ptr <<" "<< * (iter._ptr) <<Endl;5 6 iter = vecstr.erase (ITER);7 8cout << iter._ptr <<" "<< * (iter._ptr) <<Endl;9}
Once the vector's erase method is called, the previous iterator is invalidated and must be given a new iterator
Vector traversal delete a specific item and traverse Delete all project methods