一.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. ffmpeg函数02__swr_alloc_set_opts()

    SwrContext *swr_alloc(void);  // 分配重采样的上下文. SwrContext *swr_alloc_set_opts(struct SwrContext *s, int ...

  2. leetcode二刷结束

    二刷对一些常见的算法有了一些系统性的认识,对于算法的时间复杂度以及效率有了优化的意识,对于简单题和中等题目不再畏惧.三刷加油

  3. java读取word文档,提取标题和内容

    使用的工具为poi,需要导入的依赖如下 <dependency> <groupId>org.apache.poi</groupId> <artifactId& ...

  4. cookie的使用以及cookie的跨域名获取

    cookie存放容量4k左右,可设置过期时间. 1.cookie的封装使用 //设置cookies function setCookie(name, value) { var Days = 30; v ...

  5. 1502: [NOI2005]月下柠檬树

    一堆圆台平行光的投影 在草稿纸上画一下,发现对于一个圆,它投影完还是一个半径不变的圆. 定义树的轴在投影平面上经过的点为原点,定一个正方向,建立平面直角坐标系, 能发现,对于一个半径为\(r\),高度 ...

  6. CF1205C Palindromic Paths

    题目链接 问题分析 首先可以想到,坐标和为奇数的位置可以被唯一确定.同样的,如果假定\((1,2)\)是\(0\),那么坐标和为偶数的位置也可以被唯一确定.这样总共使用了\(n^2-3\)次询问. 那 ...

  7. A. Transmigration

    A. Transmigration time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  8. sklearn.model_selection Part 2: Model validation

    1. check_cv() def check_cv(cv=3, y=None, classifier=False): if cv is None: cv = 3 if isinstance(cv, ...

  9. git 指定自己的sshkey

    在服务器上生成ssh-key以后,需要把公钥放在github上,但是,这个公钥只能放在一个账户里,如果放在第二个账户里,就会提示这个key已经被用了,这是前提 一个可能的场景是这样的: 你们公司有好几 ...

  10. 《统计学习方法(李航)》讲义 第03章 k近邻法

    k 近邻法(k-nearest neighbor,k-NN) 是一种基本分类与回归方法.本书只讨论分类问题中的k近邻法.k近邻法的输入为实例的特征向量,对应于特征空间的点;输出为实例的类别,可以取多类 ...