STL之permutation/ equal_range/ binary_range学习
1,is_permutation 函数,判断其中一个序列是不是另外一个序列的全排列。
包括四个参数,前两个是第一个数组需要判断的起始位置和终止位置。后两个是第二个数组需要判断的起始位置和终止位置。
SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。 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 main(){ 7 int a[10]={1,2,3,4,5,6,7,8}; 8 int b[10]={2,1,3,5,4,6,7,8}; 9 int c[10]={3,1,2,4,5,6,7,8}; 10 int d[10]={1,2,23,4,5,6,7,6}; 11 int tmp; 12 tmp=is_permutation(a,a+2,d,d+2); // 1 13 cout<<tmp<<endl; 14 tmp=is_permutation(a,a+8,d,d+8); // 0 15 cout<<tmp<<endl; 16 tmp=is_permutation(a,a+2,d,d+4); // 0 17 cout<<tmp<<endl; 18 }
next_permutation 函数 和 prev_permutation函数的使用方法。
包括两个参数第一个是需要全排列的起始位置和终止位置。
next是求下一个大于原排列的新的排列。
prev是求下一个小于原排列的新的排列。
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 main(){ 7 int a[3]={1,2,3}; 8 next_permutation(a,a+3); 9 for(int i=0;i<3;i++){ 10 cout<<a[i]<<" "; 11 }// 1 3 2 12 cout<<endl; 13 prev_permutation(a,a+3); 14 for(int i=0;i<3;i++){ 15 cout<<a[i]<<" "; 16 }// 1 2 3 17 cout<<endl; 18 }
equal_range函数。
这个函数综合的了两个函数,第一个返回的是小于等于val的(lower_bound),第二个返回的是大于val的(upper_bound).返回的是一个pair类型的。
返回的两个分别的下标对应的数。
数组:
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 int main(){ 8 for(int i=1;i<=10;i++){ 9 a[i]=i; 10 } 11 a[5]=4; 12 // 1 2 3 4 4 6 7 8 9 10 13 auto q=equal_range(a+1,a+10+1,4); 14 cout<<a[q.first-a]<<endl;// 4 15 cout<<a[q.second-a]<<endl; // 6 16 return 0; 17 }
vector
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 vector<int>q; 7 int main(){ 8 q.push_back(1); 9 q.push_back(2); 10 q.push_back(3); 11 q.push_back(3); 12 q.push_back(4); 13 // 1 2 3 4 4 6 7 8 9 10 14 auto t=equal_range(q.begin(),q.end(),3); 15 for_each(q.begin(),t.first,[](auto i){cout<<i<<" ";});// 1 2 16 cout<<*t.first<<endl;//3 17 cout<<*t.second<<endl;// 4 18 return 0; 19 }
binary_search函数
查找当前有序区间是否存在val,如果有输出1,否则输出0
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 vector<int>q; 7 int main(){ 8 q.push_back(1); 9 q.push_back(2); 10 q.push_back(3); 11 q.push_back(3); 12 q.push_back(4); 13 // 1 2 3 4 4 6 7 8 9 10 14 int tmp=binary_search(q.begin(),q.end(),3); 15 cout<<tmp<<endl;// 1 16 tmp=binary_search(q.begin(),q.end(),5); 17 cout<<tmp<<endl;// 0 18 return 0; 19 }

更多精彩