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)