首页 > 编程语言 >C++ sort 函数 以及 priority_queue 的使用

C++ sort 函数 以及 priority_queue 的使用

时间:2023-03-25 22:22:08浏览次数:38  
标签:sort 10 return int C++ queue 升序 排序

1. sort 函数的使用

sort 函数的定义:

sort (first, end, compare);

  1. sort 对 [first, end) 范围内的元素进行排序。
  2. 默认为升序排序(此时不需要传入compare)。
  3. 当需要降序排序时,需要传入比较器 compare。

1.1 普通数组

升序

代码:

#include <iostream>
#include <algorithm>

using namespace std;

int main()
{
	int array[10];
	for(int i = 0; i < 10; i++) array[i] = 9 - i;

	printf("=====排序前=====\n");
	for(int i = 0; i < 10; i++) printf("%d ", array[i]);
	puts("");
	sort(array,array + 10);
	printf("=====排序后=====\n");
	for(int i = 0; i < 10; i++) printf("%d ", array[i]);
	puts("");

	return 0;
}

输出结果:

降序

代码:

#include <iostream>
#include <algorithm>

using namespace std;

bool cmp(int a, int b)
{
	return a > b;
}

int main()
{
	int array[10];
	for(int i = 0; i < 10; i++) array[i] = i;

	printf("=====排序前=====\n");
	for(int i = 0; i < 10; i++) printf("%d ", array[i]);
	puts("");
	sort(array,array + 10, cmp);
	printf("=====排序后=====\n");
	for(int i = 0; i < 10; i++) printf("%d ", array[i]);
	puts("");

	return 0;
}

输出结果:

此时为降序排序,需传入比较器 compare。

compare: 一个返回值为 bool 类型的函数。

如代码中 cmp 所写:

  1. 假定排序完成后的降序是从 左 到 右。
  2. 需要比较的两个数 a , b 在数组的相对位置为 a 在左, b 在右。
  3. return true 则代表二者相对位置正确,不需要改变。
  4. return false 则代表二者相对位置错位,需要改变。

1.2 结构体数组

升序

代码:

#include <iostream>
#include <algorithm>

using namespace std;

struct Student
{
	int age;
	int score;

	bool operator<(const Student& student) const
	{
		return age < student.age;
	}
};

int main()
{
	Student students[10];
	for(int i = 0; i < 10; i++)
	{
		students[i].age = 9 - i;
		students[i].score = 0;
	}
	printf("=====排序前=====\n");
	for(int i = 0; i < 10; i++) printf("%d ", students[i].age);
	puts("");
	sort(students,students + 10);
	printf("=====排序后=====\n");
	for(int i = 0; i < 10; i++) printf("%d ", students[i].age);

	return 0;
}

输出结果:

sort 默认为升序排序,当我们期望升序排序时,可以不传入 compare 比较器,只需要重载 < 运算符。

重载 < 运算符的函数返回值为 bool 类型。

  1. 假定排序完成后的升序是 从 左 到 右。
  2. *this(当前Student) 和 要比较的Student 的相对位置为 *this 在左, student 在右。
  3. return true: 二者相对位置正确,不需要改变。
  4. return fasle: 二者相对位置错误,需要改变。

降序

代码:

#include <iostream>
#include <algorithm>

using namespace std;

struct Student
{
	int age;
	int score;
};

bool cmp(const Student& a, const Student& b)
{
	return a.age > b.age;
}

int main()
{
	Student students[10];
	for(int i = 0; i < 10; i++)
	{
		students[i].age = i;
		students[i].score = 0;
	}
	printf("=====排序前=====\n");
	for(int i = 0; i < 10; i++) printf("%d ", students[i].age);
	puts("");
	sort(students,students + 10, cmp);
	printf("=====排序后=====\n");
	for(int i = 0; i < 10; i++) printf("%d ", students[i].age);
	puts("");

	return 0;
}

输出结果:

sort 默认为升序排序,当我们期望降序排序时,需要传入 compare 比较器。

如 cmp, 比较器为一个返回值为 bool 类型的函数。

  1. 假定排序完成后的降序是 从 左 到 右。
  2. Student a 和 Student b 的相对位置为 a 在左, b 在右。
  3. return true: 二者相对位置正确,不需要改变。
  4. return fasle: 二者相对位置错误,需要改变。

混合排序:

仅以一种情况作为说明。

先比较成绩,成绩高的排在前面(成绩降序),若成绩相同,则比较年龄,年龄小的排在前面(年龄升序)。

bool cmp(const Student& a, const Student& b)
{
   if(a.score == b.score)
      return a.age < b.age;
   else
      return a.score > b.score;
}

sort(students,students + 10, cmp);

sort 默认为升序排序,当我们期望混合排序时,需要传入 compare 比较器。

如 cmp, 比较器为一个返回值为 bool 类型的函数。

  1. 假定排序完成后的降序是 从 左 到 右。
  2. Student a 和 Student b 的相对位置为 a 在左, b 在右。
  3. return true: 二者相对位置正确,不需要改变。
  4. return fasle: 二者相对位置错误,需要改变。

1.3 标准库容器

以 vector 为例:

// 内置类型:int, double , string ... 等重载过 < 的类型
vector<int> vec1;

//升序:
sort(vec1.begin(),vec.end());

//降序:
bool cmp(int a, int b)
{
	return a > b;
}
sort(vec1.begin(),vec1.end(),cmp);

// 自定义类型
vector<Student> vec2;

// 升序:
struct Student
{
	int age;
	int score;

	bool operator<(const Student& student) const
	{
		return age < student.age;
	}
};
sort(vec2.begin(),vec2.end());

// 降序:
bool cmp(const Student& a, const Student& b)
{
	return a.age > b.age;
}
sort(vec2.begin(),vec2.end(),cmp);

2. priority_queue 的使用

2.1 内置类型

升序

降序

2.2 自定义类型

升序

降序

标签:sort,10,return,int,C++,queue,升序,排序
From: https://www.cnblogs.com/sachi/p/17255774.html

相关文章

  • C++ 直接初始化和拷贝初始化
    首先我们介绍直接初始化:编译器使用普通的函数匹配来选择与我们提供的参数最匹配的构造函数。文字描述可能会让你们云里雾里,那我们直接看代码://先设计这样的一个类class......
  • C++ STL摘记
    一、string类补充1.函数示例:(1)find和rfind函数,返回的是下标或者string::nposindex=ss.find(s1,pos,num)find从pos(包括)开始往右查找(num的作用待补充)index=s......
  • C/C++保安值班安排系统[2023-03-25]
    C/C++保安值班安排系统[2023-03-25]题目十五:保安值班安排系统问题描述:某公司有7名保安人员,由于工作需要进行轮休制度,一星期中每人休息一天。预先让每一个人选择自己认为......
  • C++命名规范
    C++命名规范共用准则只能是字母(A-Z,a-z)、数字(0-9)和下划线(_)组成,区分大小写文件、函数、类、变量名应当具有描述性类、自定义类型和变量名应当使用名词,函数名使用......
  • 面试高频问题之C++编译过程
    C++编译过程C++是一种高级编程语言,但是计算机并不能直接理解它。因此,需要将C++代码翻译成计算机可以理解的机器语言。这个过程就是编译过程,是C++程序从源代码到可执行文件......
  • 面试高频问题之C++编译过程
    C++编译过程C++是一种高级编程语言,但是计算机并不能直接理解它。因此,需要将C++代码翻译成计算机可以理解的机器语言。这个过程就是编译过程,是C++程序从源代码到可执行文件......
  • 设计模式之单例模式C++实现
    初步用C++实现单例模式,暂未考虑线程安全问题#include<stdio.h>#include<iostream>usingstd::cout;usingstd::cin;usingstd::endl;//单例模式的设计需求:一个......
  • C++ 用户自定义字面量(operator"" _)
    目录字面量自定义字面量定义容量字面量定义时间字面量参考字面量字面量是指源码中,固定的常量。比如,constchar*p="abcd";conststd::strings="efg";constintv......
  • C++ 2023年计算机学院”新生杯“ACM天梯赛周赛(一) 二进制转化的感悟
    题目描述对于长度为5位的一个01串,每一位都可能是0或1,一共有32种可能。它们的前几个是:0000000001000100001100100请按从小到大的顺序输出这32种01......
  • sort函数之cmp
        bool static cmp(const  pair<int,int>&a,const pair<int,int>&b)    {        return a.second < b.second ;//从小到大returna.s......