STL之partition学习

顺便存一下numeric函数的使用方法吧,感觉用处不大。

https://blog.csdn.net/baishuo8/article/details/84073565

partition函数,将元素划分为两个集合,顺序被打乱,只是分类。

包括三个参数,第一个参数代表开始位置,第二个参数代表截止位置,第三个参数代表根据什么分类。

is_partitioned 函数,判断元素是否按照给定的函数分组的,{F F F T T },{T T T T F },{TTTFF},{TTTT},{FFF}都是正确的分组。{FFTFF}是错误的分组

stable_partition函数,和partition函数差不多,分组之后只是顺序并没有被打乱。

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 # define ll long long
 4 # define inf 0x3f3f3f3f
 5 const int maxn = 2e5+100;
 6 int a[maxn];
 7 vector<int>q;
 8 bool odd(int t){
 9 return t%2==1;
10 }
11 int main()
12 {
13     for(int i=1; i<=10; i++)
14     {
15         q.push_back(i);
16     }
17     auto div=partition(q.begin(),q.end(),odd);
18 //    for(auto i=q.begin();i!=div;i++){
19 //    cout<<" "<<*i;
20 //    }
21 //    cout<<endl;
22     for_each(q.begin(),div,[](auto i){cout<<" "<<i;});
23     cout<<endl;
24     for_each(div,q.end(),[](auto i){cout<<" "<<i;});
25     cout<<endl;
26 return 0;
27 }

is_partitioned 函数

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 # define ll long long
 4 # define inf 0x3f3f3f3f
 5 const int maxn = 2e5+100;
 6 int a[maxn];
 7 vector<int>q;
 8 bool odd(int t){
 9 return t%2==1;
10 }
11 int main()
12 {
13     for(int i=1; i<=10; i++)
14     {
15         q.push_back(i);
16     }
17 
18     cout<<is_partitioned(q.begin(),q.end(),odd)<<endl;//   0
19        auto div=partition(q.begin(),q.end(),odd);
20     cout<<is_partitioned(q.begin(),q.end(),odd)<<endl;//  1
21 return 0;
22 }

stable_partitioned 函数

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 # define ll long long
 4 # define inf 0x3f3f3f3f
 5 const int maxn = 2e5+100;
 6 int a[maxn];
 7 vector<int>q;
 8 bool odd(int t){
 9 return t%2==1;
10 }
11 int main()
12 {
13     for(int i=1; i<=10; i++)
14     {
15         q.push_back(i);
16     }
17        auto div=stable_partition(q.begin(),q.end(),odd);
18        for_each(q.begin(),div,[](auto i ){cout<<i<<" ";});
19        cout<<endl;
20        for_each(div,q.end(),[](auto i){cout<<i<<" ";});
21 return 0;
22 }
原文地址:https://www.cnblogs.com/letlifestop/p/10678764.html