To continue the traversal process after the element is removed while traversing std::list

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.