一.sort函数

1.sort函数包含在头文件为#include<algorithm>的c++标准库中,调用标准库里的排序方法可以实现对数据的排序,但是sort函数是如何实现的,我们不用考虑!

2.sort函数的模板有三个参数:

void sort (RandomAccessIterator first, RandomAccessIterator last, Compare comp);

(1)第一个参数first:是要排序的数组的起始地址。

(2)第二个参数last:是结束的地址(最后一个数据的后一个数据的地址)

(3)第三个参数comp是排序的方法:可以是从升序也可是降序。如果第三个参数不写,则默认的排序方法是从小到大排序。

3.实例

 #include<iostream>
#include<algorithm>
using namespace std;
main()
{
  //sort函数第三个参数采用默认从小到大
  int a[]={,,,,,,,,,};
  sort(a,a+);
  for(int i=;i<;i++)
  cout<<a[i]<<" ";
}

运行结果:

 #include<iostream>
#include<algorithm>
using namespace std;
bool cmp(int a,int b);
main(){
  //sort函数第三个参数自己定义,实现从大到小
  int a[]={,,,,,,,,,};
  sort(a,a+,cmp);
  for(int i=;i<;i++)
    cout<<a[i]<<" ";
}
//自定义函数
bool cmp(int a,int b){
  return a>b;
}

运行结果:

 #include<iostream>
#include<algorithm>
#include"cstring"
using namespace std;
typedef struct student{
  char name[];
  int math;
  int english;
}Student;
bool cmp(Student a,Student b);
main(){
  //先按math从小到大排序,math相等,按english从大到小排序
  Student a[]={{"apple",,},{"limei",,},{"apple",,}};
  sort(a,a+,cmp);
  for(int i=;i<;i++)
  cout<<a[i].name <<" "<<a[i].math <<" "<<a[i].english <<endl;
}
bool cmp(Student a,Student b){
  if(a.math >b.math )
  return a.math <b.math ;//按math从小到大排序
  else if(a.math ==b.math )
      return a.english>b.english ; //math相等,按endlish从大到小排序
}

运行结果

4.对于容器,容器中的数据类型可以多样化

1) 元素自身包含了比较关系,如int,double等基础类型,可以直接进行比较greater<int>() 递减, less<int>() 递增(省略)

 #include<iostream>
#include<algorithm>
#include"vector"
using namespace std;
typedef struct student{
char name[];
int math;
int english;
}Student;
bool cmp(Student a,Student b);
main(){
int s[]={,,,,};
vector<int>arr(s,s+);
sort(arr.begin(),arr.end(),greater<int>());
for(int i=;i<arr.size();i++)
cout<<arr[i]<<" ";
}

运行结果:

2)元素本身为class或者struct,类内部需要重载< 运算符,实现元素的比较;

注意事项:bool operator<(const className & rhs) const;  如何参数为引用,需要加const,这样临时变量可以赋值;重载operator<为常成员函数,可以被常变量调用;

 #include<iostream>
#include<algorithm>
#include"vector"
using namespace std;
typedef struct student{
char name[];
int math;
//按math从大到小排序
inline bool operator < (const student &x) const {
return math>x.math ;
}
}Student;
main(){
Student a[]={{"apple",},{"limei",},{"apple",}};
sort(a,a+);
for(int i=;i<;i++)
cout<<a[i].name <<" "<<a[i].math <<" " <<endl;
}

运行结果:

重载<也可以定义为如下格式:

 struct Cmp{
bool operator()(Info a1,Info a2) const {
return a1.val > a2.val;
}
};

C++中sort函数使用方法的更多相关文章

  1. vlookup函数基本使用--如何将两个Excel表中的数据匹配;excel表中vlookup函数使用方法将一表引到另一表

    vlookup函数基本使用--如何将两个Excel表中的数据匹配:excel表中vlookup函数使用方法将一表引到另一表 一.将几个学生的籍贯匹配出来‘ 二.使用查找与引用函数 vlookup 三. ...

  2. 查看dll中的函数(方法)

    https://jingyan.baidu.com/article/5553fa82b953b365a23934b7.html 查看dll中的函数(方法) 听语音 | 浏览:2004 | 更新:201 ...

  3. shell 从函数文件中调用函数的方法

    你可以把所有的函数存储在一个函数文件中 你可以把所有的文件函数加载到当前脚本或命令行 加载函数文件中所有函数的方法: source xxx.sh

  4. qsort函数以及sort函数使用方法

     sort函数的使用方法 做ACM题的时候,排序是一种常常要用到的操作. 假设每次都自己写个冒泡之类的O(n^2)排序,不但程序easy超时,并且浪费宝贵的比赛时间,还非常有可能写错. STL里面 ...

  5. STL 之 sort 函数使用方法

    关于Sort Sort函数是C++ STL(Standard Template Library / 标准函数库) <algorithm>头文件中的一个排序函数,作用是将一系列数进行排序,因 ...

  6. 『嗨威说』算法设计与分析 - STL中Sort函数的实现原理初探

    本文索引目录: 一.对Sort算法实现的个人阅读体会 二.Sort算法使用的三个排序算法的优点介绍 2.1 插入排序的优缺点 2.2 堆排序的优缺点 2.3 快速排序的优缺点 2.4 新的结合排序—— ...

  7. <algorithm>中sort()函数的用法

    先说一下,本篇文章我没有讲sort()实现排序的原理,我写在另一篇文章中了,如果想了解的话,可以看一下,附上链接:https://www.cnblogs.com/buanxu/p/12772700.h ...

  8. 【C++】sort函数使用方法

    一.sort函数 1.sort函数包含在头文件为#include<algorithm>的c++标准库中,调用标准库里的排序方法可以实现对数据的排序,但是sort函数是如何实现的,我们不用考 ...

  9. c/c++中sort函数用法

    转载自博主:九江镇中https://www.cnblogs.com/jjzzx/ c++标准库里的排序函数的使用方法 I)Sort函数包含在头文件为#include<algorithm>的 ...

随机推荐

  1. ELK是什么?

    ELK = ElasticSearch + Logstash + Kibana  Elasticsearch:后台分布式存储以及全文检索 Logstash :  日志加工.“搬运工” Kibana : ...

  2. sqlalchemy 中的get_or_404

  3. IC SPEC相关数据

    ---恢复内容开始--- 静态电流:静态电流是指没有信号输入时的电流,也就是器件本身在不受外部因素影响下的本身消耗电流. 纹波电压的害处: 1.容易在用设备中产生不期望的谐波,而谐波会产生较多的危害: ...

  4. springboot 开启缓存

    Caching Data with Spring This guide walks you through the process of enabling caching on a Spring ma ...

  5. zabbix监控win服务器

    https://jingyan.baidu.com/article/fcb5aff76486f2edaa4a712a.html 卸载win上的zabbix: cmd /c "C:\zabbi ...

  6. 部署安装snort--入侵检测工具

    1:部署安装snort yum -y install wget 2: 基本依赖环境 yum -y install gcc flex bison zlib zlib-devel libpcap libp ...

  7. MySQL的字符集操作命令总结

    这篇文章主要介绍了MySQL的字符集操作命令总结,包括各种查看数据库.数据表等查询命令,需要的朋友可以参考下   以下均在mysql 5.5命令行中运行通过: 查看MySQL支持的字符集: 代码如下: ...

  8. 暑假集训 #2 div1 I - Lada Priora 精度处理

    I - Lada Priora Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Sub ...

  9. Selenium 加载Chrome/Firefox浏览器配置文件

    Selenium启动浏览器时,默认是打开一个新用户,不会加载原有的配置以及插件.但有些时候我们可能需要加载默认配置. 一.Chrome浏览器 1.在Chrome浏览器的地址栏输入:chrome://v ...

  10. 「SCOI2015」小凸玩矩阵

    题目链接 问题分析 题目给了充足的暗示,我们只需要二分答案然后跑匈牙利即可.要相信匈牙利的速度 参考程序 #include <bits/stdc++.h> using namespace ...