STL--Remove and remove_if ()

Source: Internet
Author: User

Remove and remove_if ()

Remove () function

Remove (beg,end,const t& value)  // remove interval {beg,end) for each element "equal to value";

Remove is only removed by moving the iterator pointer forward, placing the element that is not deleted in front of the list, and returning an iterator that points to the new, extra-tailed value. Because the Remove () function is not a member, you cannot adjust the length of the linked list. The Remove () function is not really a delete, but you can use the erase () or resize () function to actually delete the element. Use the following:

string " Text with some   spaces""),  str1.end ());    "Textwithsomespaces"

Function Prototypes:

class class T >const t& value) {    = std::find (First, last, value);     if (First! = Last )          for (ForwardIt i = first; ++i! = last ;            ) if (! (*i = = value)                ) *first++ = Std::move (*i);     return First ;}

Remove_if() function

Remove_if (Beg, end, op)   // remove interval [beg,end) for each "make-to-judge: OP (Elem) obtains true" element;

The parameters of the remove_if (remove and unique are the same) are iterators, the container itself cannot be obtained through iterators, and the elements inside the container can only be removed through the container's member functions, so the Remove series function cannot actually delete the element. You can only move the element that you want to delete to the end of the container and return the iterator to which you want to delete the element, and then delete it by erase the member function. Use the following:

BOOL IsSpace (charreturn';}
string " Text with some spaces"; Str2.erase (remove_if (Str2.begin (), // "Textwithsomespaces"

Function Prototypes:

template<classclass unarypredicate>forwardit remove_if (forwardit First, ForwardIt last, Unarypredicate p) {    = std::find_if (First, last, p);     if (First! = Last )          for (ForwardIt i = first; ++i! = last ;            ) if (!p (*i))                 *first++ = Std::move (*i);     return First ;}

STL--Remove and remove_if ()

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.