码迷,mamicode.com
首页 > 编程语言 > 详细

从0开始学算法--排序(1.12c++利用标准库排序)

时间:2019-10-17 14:16:07      阅读:87      评论:0      收藏:0      [点我收藏+]

标签:开始   标准库   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);

 

从0开始学算法--排序(1.12c++利用标准库排序)

标签:开始   标准库   cst   math   double   结构体排序   ios   str   class   

原文地址:https://www.cnblogs.com/wz-archer/p/11691446.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!