首页 > 代码库 > STL algorithm算法partition, partition_copy(43)
STL algorithm算法partition, partition_copy(43)
partition原型:
td::partition
- C++98
- C++11
template <class ForwardIterator, class UnaryPredicate>
ForwardIterator partition (ForwardIterator first,
ForwardIterator last, UnaryPredicate pred);
返回值为第一个返回值为false元素的迭代器,如果不存在返回false的元素,则返回last.
划分的标准有三种情况!
1.全部为true;
2.前半部分为true,后半部分为false.
3.全部为false.
注意,前半部分为false,后半部分为true不属于一个划分!!!
详看:http://blog.csdn.net/qq844352155/article/details/39318925
其行为类似如下:
template <class BidirectionalIterator, class UnaryPredicate> BidirectionalIterator partition (BidirectionalIterator first, BidirectionalIterator last, UnaryPredicate pred) { while (first!=last) { while (pred(*first)) { ++first; if (first==last) return first; } do { --last; if (first==last) return first; } while (!pred(*last)); swap (*first,*last); ++first; } return first;
一个简单的测试例子:
#include <iostream> #include <vector> #include <array> #include <algorithm> using namespace std; int main(){ vector<int> vi{8,5,10,7,1,3,5,8,9,13}; cout<<"vi="; for_each(vi.begin(),vi.end(),[](int i){cout<<i<<" ";}); cout<<endl; if(is_partitioned(vi.begin(),vi.end(),[](int n){return n%2==0;})) cout<<"v1 is a partitioned!"<<endl; else cout<<"v1 not a partitioned!"<<endl; auto it=partition(vi.begin(),vi.end(),[](int n){return n%2==0;}); cout<<"after auto it=partition(vi.begin(),vi.end(),[](int n){return n%2==0;}); "<<endl; cout<<"vi="; for_each(vi.begin(),vi.end(),[](int i){cout<<i<<" ";}); cout<<endl; if(is_partitioned(vi.begin(),vi.end(),[](int n){return n%2==0;})) cout<<"v1 is a partitioned!"<<endl; else cout<<"v1 not a partitioned!"<<endl; cout<<"*it="<<*it<<endl; }运行截图:
partition_copy原型:
std::partition_copy
template <class InputIterator, class OutputIterator1,
class OutputIterator2, class UnaryPredicate pred>
pair<OutputIterator1,OutputIterator2>
partition_copy (InputIterator first, InputIterator last,
OutputIterator1 result_true, OutputIterator2 result_false,
UnaryPredicate pred);
该函数是将范围内的元素根据调用pred返回值的不同,将元素分别复制到result_true,和resutl_false所指向的容器内。
返回值是一个pair,分别指向result_true,和resutl_false所覆盖最后一个元素的下一个元素位置的迭代器。
其行为类似如下:
template <class InputIterator, class OutputIterator1, class OutputIterator2, class UnaryPredicate pred> pair<OutputIterator1,OutputIterator2> partition_copy (InputIterator first, InputIterator last, OutputIterator1 result_true, OutputIterator2 result_false, UnaryPredicate pred) { while (first!=last) { if (pred(*first)) { *result_true = *first; ++result_true; } else { *result_false = *first; ++result_false; } ++first; } return std::make_pair (result_true,result_false); }一个简单的例子:
#include <iostream> #include <vector> #include <array> #include <algorithm> using namespace std; int main(){ vector<int> vi{8,5,10,7,1,3,5,8,9,13}; vector<int> vTrue(10); vector<int> vFalse(10); cout<<"vi="; for_each(vi.begin(),vi.end(),[](int i){cout<<i<<" ";}); cout<<endl; auto it=partition_copy(vi.begin(),vi.end(),vTrue.begin(),vFalse.begin(),[](int n){return n%2==0;}); cout<<"after partition_copy(vi.begin(),vi.end(),vTrue.begin(),vFalse.begin(),[](int n){return n%2==0;}) "<<endl; cout<<"vi="; for_each(vi.begin(),vi.end(),[](int i){cout<<i<<" ";}); cout<<endl; cout<<"vTrue="; for_each(vTrue.begin(),vTrue.end(),[](int i){cout<<i<<" ";}); cout<<endl; cout<<"vFalse="; for_each(vFalse.begin(),vFalse.end(),[](int i){cout<<i<<" ";}); cout<<endl; cout<<"*(it.first-1)="<<*(it.first-1)<<endl; cout<<"*(it.second-1)="<<*(it.second-1)<<endl; }运行截图:
——————————————————————————————————————————————————————————————————
//写的错误或者不好的地方请多多指导,可以在下面留言或者点击左上方邮件地址给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。
转载请注明出处:http://blog.csdn.net/qq844352155
author:天下无双
Email:coderguang@gmail.com
2014-9-22
于GDUT
——————————————————————————————————————————————————————————————————
STL algorithm算法partition, partition_copy(43)