Valid STL: Methods for deleting elements from different containers

Source: Internet
Author: User
How to delete elements from different containers
  • Remove all objects with specific values in a container:
    If the container is a vector, string, or deque, use erase-Remove.
    If the container is list, use list: Remove.
    If the container is a standard associated container, use its erase member function.

  • Remove all objects in a container that meet a specific criterion:
    If the container is a vector, string, or deque, use a erase-remove_if.
    If the container is list, use list: remove_if.
    If the container is a standard associated container, write a loop to traverse the container elements. Remember when you pass the iterator to erasePost IncrementIt (after an element in erase, The iterator pointing to this element becomes invalid, and all the iterators that need to point to the next element before erase ).

    Assoccontainer <int> C;... For (assoccontainer <int>: iterator I = C. Begin (); I! = C. End ();/* Nothing */)// The third part of the For Loop is empty.{If (badvalue (* I) {C. Erase (I ++ );// For bad values, pass the current I to erase, and then increase I as a side effect;} Else {++ I;// For a good value, only add I}}
  • Do something in the loop (except Delete objects ):
    If the container is a standard sequence container, write a loop to traverse the container elements. RememberUpdate iterator with its return value.

    If the container is a standard associated container, write a loop to traverse the container elements. Remember when you pass the iterator to erasePost IncrementIt



Valid STL: Methods for deleting elements from different containers

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.