STLalgorithm算法remove_copy,remove_copy_if(48)
remove_copy原型:
<span style="font-size:18px;">std::remove_copy template <class InputIterator, class OutputIterator, class T> OutputIterator remove_copy (InputIterator first, InputIterator last, OutputIterator result, const T& val);</span>
该函数是将范围[first,last)里面,值不等于val的元素逐一复制到从result开始的位置。
复制之后,范围[first,last)内的元素不变,从result开始到最后一个被覆盖的元素被改变。
返回值为result范围内最后一个被覆盖元素的下一个位置的迭代器。
其行为类似于:
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; }
一个简单的例子:
#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; }
运行截图:
remove_copy_if原型:
std::remove_copy_if
template <class InputIterator, class OutputIterator, class UnaryPredicate> OutputIterator remove_copy_if (InputIterator first, InputIterator last, OutputIterator result, UnaryPredicate pred);
remove_copy_if和remove_copy的区别和remove_if和remove的区别是非常相似的。
是将除了pred返回值为true的元素都复制到result开始的位置。
行为类似于:
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; }
一个简单的例子:
#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; }
运行截图:
——————————————————————————————————————————————————————————————————
//写的错误或者不好的地方请多多指导,可以在下面留言或者点击左上方邮件地址给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。
转载请注明出处:http://blog.csdn.net/qq844352155
author:天下无双
Email:coderguang@gmail.com
2014-9-25
于GDUT
——————————————————————————————————————————————————