sort类函数:
| 函数名 | 功能描述 | 
|---|---|
| sort | 对给定区间所有元素进行排序 | 
| stable_sort | 对给定区间所有元素进行稳定排序 | 
| partial_sort | 对给定区间所有元素部分排序 | 
| partial_sort_copy | 对给定区间复制并排序 | 
| nth_element | 找出给定区间的某个位置对应的元素 | 
| is_sorted | 判断一个区间是否已经排好序 | 
| partition | 使得符合某个条件的元素放在前面 | 
| stable_partition | 相对稳定的使得符合某个条件的元素放在前面 | 
标签:均衡 互联网 工作 资源 原因 之间 模糊 公司 设计
需要头文件<algorithm>
语法描述:sort(begin,end,cmp),cmp参数可以没有,如果没有默认非降序排序。
默认情况下例子:以int类型的数据为例子
#include<iostream> #include<algorithm> #include<cstring> using namespace std; int main() { int a[5]={1,3,4,2,5}; sort(a,a+5); for(int i=0;i<5;i++) cout<<a[i]<<‘ ‘; return 0; }
functional提供了一堆基于模板的比较函数对象。它们是:equal_to<Type>、not_equal_to<Type>、greater<Type>、greater_equal<Type>、less<Type>、less_equal<Type>。
#include<iostream> #include<bits/stdc++.h> using namespace std; int main(){ int a[5]={1,3,4,2,5}; sort(a,a+5); for (int i = 0; i < 5; i++) { cout<<a[i]<<" ";//1 2 3 4 5 } cout<<endl; sort(a,a+5,less<int>() ); for (int i = 0; i < 5; i++) { cout<<a[i]<<" ";//1 2 3 4 5 } cout<<endl; sort(a,a+5,greater<int>() ); for (int i = 0; i < 5; i++) { cout<<a[i]<<" ";//5 4 3 2 1 } }
输出结果:
1 2 3 4 5
1 2 3 4 5
5 4 3 2 1
#include<iostream> #include<algorithm> #include<cstring> using namespace std; int main() { string str("hello world"); sort(str.begin(),str.end()); cout<<str; return 0; }
结果:空格dehllloorw
#include<iostream> #include<algorithm> #include<cstring> using namespace std; int main() { string str("hello world"); sort(str.rbegin(),str.rend()); cout<<str; return 0; }
结果:wroolllhde空格
#include<iostream> #include<cstring > #include<algorithm> using namespace std; int main() { string a[4]; for(int i=0;i<4;i++) getline(cin,a[i]); sort(a,a+4); for(int i=0;i<4;i++) cout<<a[i]<<endl; return 0; }
#include<iostream> #include<algorithm> #include<cstring> using namespace std; struct link { int a,b; }; bool cmp(link x,link y) { if(x.a==y.a) return x.b>y.b; return x.a>y.a; } int main() { link x[4]; for(int i=0;i<4;i++) cin>>x[i].a>>x[i].b; sort(x,x+4,cmp); for(int i=0;i<4;i++) cout<<x[i].a<<‘ ‘<<x[i].b<<endl; return 0; }
class Solution { #define N 10010 int bit[N]; public: int get(int x){ int res=0; while (x) res+=x&1,x>>=1; return res; } vector<int> sortByBits(vector<int>& arr) { for (auto x:arr) bit[x]=get(x); sort(arr.begin(),arr.end(),[&](int x,int y){ return bit[x]==bit[y]?x<y:bit[x]<bit[y]; }); return arr; } };
参见:https://www.cnblogs.com/BlairGrowing/p/13459973.html
标签:均衡 互联网 工作 资源 原因 之间 模糊 公司 设计
原文地址:https://blog.51cto.com/11976981/2517853