STL algorithm remove_copy, remove_copy_if (48)

Source: Internet
Author: User

Remove_copy prototype:

<span style="font-size:18px;">std::remove_copytemplate <class InputIterator, class OutputIterator, class T>  OutputIterator remove_copy (InputIterator first, InputIterator last,                              OutputIterator result, const T& val);</span>

This function copies the elements with values not equal to Val in the range [first, last) to the position starting from result one by one.

After copying, the elements in the range [first, last) remain unchanged, and the elements overwritten from result to last are changed.

The returned value is the iterator of the next position of the last element to be overwritten in the result range.

The behavior is similar:

template <class InputIterator, class OutputIterator, class T>  OutputIterator remove_copy (InputIterator first, InputIterator last,                              OutputIterator result, const T& val){  while (first!=last) {    if (!(*first == val)) {      *result = *first;      ++result;    }    ++first;  }  return result;}
A simple example:

#include <iostream>#include <algorithm>#include <vector>using namespace std;void removeif(){    vector<int> vi{1,2,3,4,5,6};    vector<int> vresult(6);    cout<<"vi=";    for(int i:vi)        cout<<i<<" ";    cout<<endl;    cout<<"vresult=";    for(int i:vresult)        cout<<i<<" ";    cout<<endl;    cout<<"vi.size()="<<vi.size()<<endl;    cout<<"vresult.size()="<<vresult.size()<<endl;    auto it=remove_copy(vi.begin(),vi.end(),vresult.begin(),5);    cout<<"after auto it=remove_copy(vi.begin(),vi.end(),vresult.begin(),5);"<<endl;    cout<<"vi=";    for(int i:vi)        cout<<i<<" ";    cout<<endl;    cout<<"vresult=";    for(int i:vresult)        cout<<i<<" ";    cout<<endl;    cout<<"vi.size()="<<vi.size()<<endl;    cout<<"vresult.size()="<<vresult.size()<<endl;    cout<<"it="<<*it<<endl;    cout<<"*(it-1)="<<*(it-1)<<endl;}
Run:









Remove_copy_if prototype:

STD: remove_copy_if
template <class InputIterator, class OutputIterator, class UnaryPredicate>  OutputIterator remove_copy_if (InputIterator first, InputIterator last,                                 OutputIterator result, UnaryPredicate pred);
The difference between remove_copy_if and remove_copy is very similar to that between remove_if and remove.

Is to copy all elements except the elements whose return value is true to the start position of result.

The behavior is similar:

template <class InputIterator, class OutputIterator, class UnaryPredicate>  OutputIterator remove_copy_if (InputIterator first, InputIterator last,                                 OutputIterator result, UnaryPredicate pred){  while (first!=last) {    if (!pred(*first)) {      *result = *first;      ++result;    }    ++first;  }  return result;}
A simple example:

#include <iostream>#include <algorithm>#include <vector>using namespace std;void removecopyif(){    vector<int> vi{1,2,3,4,5,6};    vector<int> vresult(6);    cout<<"vi=";    for(int i:vi)        cout<<i<<" ";    cout<<endl;    cout<<"vresult=";    for(int i:vresult)        cout<<i<<" ";    cout<<endl;    cout<<"vi.size()="<<vi.size()<<endl;    cout<<"vresult.size()="<<vresult.size()<<endl;    auto it=remove_copy_if(vi.begin(),vi.end(),vresult.begin(),[](int n){return n%2==0;});    cout<<"after auto it=remove_copy(vi.begin(),vi.end(),vresult.begin(),[](int n){return n%2==0;});"<<endl;    cout<<"vi=";    for(int i:vi)        cout<<i<<" ";    cout<<endl;    cout<<"vresult=";    for(int i:vresult)        cout<<i<<" ";    cout<<endl;    cout<<"vi.size()="<<vi.size()<<endl;    cout<<"vresult.size()="<<vresult.size()<<endl;    cout<<"it="<<*it<<endl;    cout<<"*(it-1)="<<*(it-1)<<endl;}
Run:






------------------------------------------------------------------

// For more instructions on writing errors or poor information, you can leave a message below or click the email address in the upper left corner to send an email to me, pointing out my errors and deficiencies, so that I can modify them, thank you for sharing it.

Reprinted please indicate the source: http://blog.csdn.net/qq844352155

Author: unparalleled

Email: [email protected]

Yu gdut

--------------------------------------------------






STL algorithm remove_copy, remove_copy_if (48)

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.