Std::list::erase
Erase elements
Removes from the list container either a single element (position) or a range of elements ([First,last)).
This effectively reduces the container size by the number of elements removed, which is destroyed.
Unlike other standard sequence containers, list and Forward_list objects is specifically designed to be efficient inserti Ng and removing elements in any position, even in the middle of the sequence.
return value
An iterator pointing to the element, the followed the last element, erased by the function call. The container End if the operation erased the last element in the sequence.
intiarray[Ten] = {2,3,6,4,1, -,4,9, -,4 }; List<int> iList (iarray, IArray +Ten); Vector<int>IVec; for(list<int>::iterator it =Ilist.begin (); It! = Ilist.end (); it++) {Ivec.push_back (*it); if(*it = =4) {It=Ilist.erase (IT); It--; }} cout<<"IVec:"; for(auto&I:ivec) {cout<< I <<" "; } cout<<Endl; cout<<"iList:"; for(auto&i:ilist) {cout<< I <<" "; } cout<< Endl;
Output:
Ivec:2 3 6 4 1 17 4 9 25 4
Ilist:2 3 6 1 17 9 25
Or:
for (list<int>::iterator it = ilist.begin (); ! = ilist.end ();) { Ivec.push_back (*it); if 4 ) { = ilist.erase (it); Else { It+ +; } }
To continue the traversal process after the element is removed while traversing std::list