一.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. cyopen注释掉导入的动态函数

    cyopen注释掉导入的动态函数 cyopen注释掉导入的动态函数 cyopen注释掉导入的动态函数

  2. java获取音频文件播放时长

    方法一: 在项目开发过程中,需要获取音视频文件时长.查询资料后发现 JAVE能够完美得到想要的结果,JAVE项目简介如下: The JAVE (Java Audio Video Encoder) li ...

  3. 输出1~n中1的个数

    //输出1~n中1的个数,如f(1)=1,f(13)=6.通过测试,bymyself public class FindOnes{ public static void main(String arg ...

  4. <?xml version="1.0" encoding="UTF-8" ?>的意思

    <?xml version="1.0" encoding="UTF-8" ?> ?xml 这里是申明文件类型,这申明的是XML文件:version= ...

  5. os.system 的坑,'C:\Program' 不是内部或外部命令,也不是可运行的程序 或批处理文件

    首先对os.system()是执行一些系统命令,参数是以字符串的形式进行传递,如果有多个参数时,用空格隔开 例子1:cd 和 D:用空格间隔开来,代表两个参数 但一些情况空格只是字符串里面组成部分,不 ...

  6. Nagios-报错:UNKNOWN Can't connect to the JVM:

    原因: 由于手动开启nrpe程序,产生临时文件,需要把产生的多余文件删除. [root@nagios ~]# ll /tmp/drwx------ 3 root root 17 Aug 12 13:4 ...

  7. inline元素导航栏案例

    练习一个超链接元素在文档流当中,a标签显示出来的inline这种元素的特性.我们可以用display来将它转换成inline-block类型,这样我们就可以设置它的高度,宽度和它的背景颜色等等特性了. ...

  8. 静态库lib、动态库dll基础

    首先从hello world!开始 //main.cpp文件 void cpu(); int main() { put(); ; } 在main.cpp中定义了一个cpu():函数,但没有实现其功能, ...

  9. 16位masm汇编实现筛法,状压求十万以内素数

    .model small .data table byte 3,12500 dup (0);;0和1不是质数 i word 0 j word 0 .stack 4096 .code main proc ...

  10. python中assert的用法

    assert:断言 格式: assert 表达式 [, 参数] 当表达式为真时,程序继续往下执行: 当表达式为假时,抛出AssertionError错误,并将  参数  输出 举例: def foo( ...