标签:开始 标准库 cst math double 结构体排序 ios str class
1,简单数组按升序排序
sort(a,a+n);
#include <algorithm> #include <iostream> #include <cstring> #include <vector> #include <cstdio> #include <cmath> #include <queue> using namespace std; const int maxn=1e5+10; int a[maxn]; int n; int main(){ scanf("%d",&n); for(int i=0;i<n;i++){ scanf("%d",&a[i]); } sort(a,a+n); for(int i=0;i<n;i++){ printf("%d ",a[i]); }printf("\n"); return 0; }
2.简单数组降序排序
#include <algorithm> #include <iostream> #include <cstring> #include <vector> #include <cstdio> #include <cmath> #include <queue> using namespace std; const int maxn=1e5+10; int a[maxn]; int n; bool complare(int a,int b){ return a>b; } int main(){ scanf("%d",&n); for(int i=0;i<n;i++){ scanf("%d",&a[i]); } sort(a,a+n,complare); for(int i=0;i<n;i++){ printf("%d ",a[i]); }printf("\n"); return 0; }
3.结构体排序
struct node{ int a,b; double c; }an[maxn]; bool cmp(node x,node y){ if(x.a!=y.a)return x.a<y.a; if(x,b!=y.b)return x.b>y.b; return x.c<y.c; } sort(an,an+n,cmp);
4.vector排序
sort(v.begin(),v.end());
5.vector结构体排序
struct job{ int j,b; bool operator<(cost job &x)const{ return j>x.j; } }; vector<job>v; sort(v.begin(),v.end);
标签:开始 标准库 cst math double 结构体排序 ios str class
原文地址:https://www.cnblogs.com/wz-archer/p/11691446.html