C++ sort()

 

C++中sort()函数的特性:

  • 标准函数sort() 的头文件:#include<algorithm>              std::sort;
  • sort() 默认是按元素从小到大排序;
  • sort() 能用到vector和set中,vector是线性容器,set是关联容器;
  • sort() 可以对静态的数组进行排序;
  • C++中自带sort() 排序方法,需要提供待排序序列的头地址、尾地址、排序方式(降序或升序);
  • sort() 时间复杂度为Nlog2(N);
  • vector,set,map这些容器需要用prev(xxx.end())才能取出容器中最后一个元素。
错误:cout <<" a.end()"<< *a.end() << endl;//执行这句话会报错!
cout << " prev(a.end)" << *prev(a.end()) << endl;//正确取出最后一个元素

语法:sort(start,end,ways)

  • start:序列头地址
  • end:序列尾地址
  • ways:排序方式,升序或降序,默认为升序。可自定义,也可以使用内置排序方法:less<数据类型>(),greater<数据类型>()   包含在std::less    std::greater中

例如:

(1)对静态数组排序

#include<iostream>
#include<algorithm>
using namespace std;

//对静态数组排序
int main()
{
    int a[5]={9,8,7,6,5};
    sort(a,a+10);  //默认排序为升序,a为首地址,a+10为尾地址(取不到)
    for(int i=0; i<5; i++)
        cout<<a[i]<<" ";
    return 0;
}

输出:5 6 7 8 9

(2)自定义排序

#include<iostream>
#include<algorithm>
using namespace std;

//自定义排序方式:告诉程序从大到小地排序
bool complare(int a,int b)
{
 return a>b;
}
int main()
{
    int a[10]={1,2,3,4,5};
    sort(a,a+5,compare);//compare不需要传入参数
    for(int i=0;i<5;i++)
        cout<<a[i]<<" ";
    return 0;
}

输出:5 4 3 2 1

(3)对vector容器排序 (用迭代器实现,string类似)

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;

int my_cmp(pair<string,int> p1, pair<string,int> p2)
{
    return p1.second > p2.second;//按值从大到小排序
}
int main()
{
    vector<pair<string,int>> vec;
    vec.push_back(make_pair("s1",1));
    vec.push_back(make_pair("s2",3));
    vec.push_back(make_pair("s3",2));
    sort(vec.begin(), vec.end(), my_cmp); //vector用迭代器实现
    cout<<vec[0].first<<"  "<<vec[1].first<<"  "<<vec[2].first<<endl;
}

输出:s1 s3 s2

(4)对set容器排序 (用迭代器实现)

  • set是集合,内部的元素不会重复,同时它会自动进行排序,默认从小到大,
  • set的insert方法没有insert(a,cmp)这种重载,所以如果要把结构体插入set中,我们就要重载'<'运算符。
  • set方法在插入的时候也是从小到大的,那么我们重载一下<运算符让它从大到小排序
#include <iostream>
#include<string>
#include<algorithm>
#include<set>
using namespace std;

struct student
{
    char name[10];
    int score;
};
//自定义“大于”
bool comp(const student &a, const student &b){
    return a.score > b.score;
}
bool operator > (const student & stu1, const student &stu2){
    return stu1.score > stu2.score;
}
int main()
{
    set<student> setStudents;
    setStudents = {, };
    int n = 2;
    while (n--)
    {
        student oneStudent;
        string name;
        int score;
        cin >> name >> score;
        strcpy(oneStudent.name, name.c_str());
        oneStudent.score = score;
        setStudents.insert(oneStudent);
    }
    cout<<"排序前: "<<endl;
    (set<student>::iterator it = setStudents.begin(); it != setStudents.end(); it++)
    {
        cout<<it->name<<it->score<<endl;
    }
    sort(setStudents.begin(), setStudents.end(), comp);
    cout<<"排序后:"<<endl;
    for (set<student>::iterator it = setStudents.begin(); it != setStudents.end(); it++)
    {
        cout<<it->name<<it->score<<endl;
    }
    return 0;
}

输出:
排序前:
二明 98
大明 99

排序后:
大明 99
二明 98

参考:https://www.cnblogs.com/zhouxiaosong/p/5557990.html

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值