C++小知识——sort函数

一、sort函数

使用方法

1.需要包含头文件,并使用命名空间std

#include <algorithm>
using namespace std;

2. sort函数原型,排序范围[A,B)

在这里插入图片描述


/**
* @该函数用于排序
* @param param1 要排序的起始地址
* @param param2 要排序的结束地址
* @param param3 排序的方法
* @return 返回说明
*     -<em>void</em>
*/
template <class RandomAccessIterator>
void sort(RandomAccessIterator first, RandomAccessIterator last);

template <class RandomAccessIterator, class Compare>
void sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp);

3.使用实例

3.1 两个参数对向量和数组进行排序

1.测试代码

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>

template <class Type>
void printArray(Type Array[], int size){
	for (size_t i = 0; i < size; ++i) {
		std::cout << Array[i] << " ";
	}
	std::cout << std::endl << std::endl;
}

template <class Type>
void printVector(const std::vector<Type> &V) {
	for (auto i : V){
		std::cout << i << " ";
	}
	std::cout << std::endl << std::endl;
}

int main(int argc, char* argv[]) {

	std::vector<int> a{ 1, 3, 51, 22, 4, 6, 9, 0 };
	std::string b[8] = { "a", "c", "d", "e", "b", "g", "f", "h" };
	
	std::cout << "排序前: " << std::endl;
	std::cout << "a: ";
	printVector(a);
	std::cout << "b: ";
	printArray(b, 8);

	std::cout << "-------------↓↓两参数默认升序排列↓↓-------------" << std::endl;	
	std::sort(a.begin(), a.end());
	std::sort(b, b + 8);

	std::cout << "排序后: " << std::endl;
	std::cout << "a: ";
	printVector(a);
	std::cout << "b: ";
	printArray(b, 8);

	return 0;
}

2.结果以及分析
在这里插入图片描述

那么,假如我将范围缩小,从0开始,不到最末尾,结果会是怎样呢?也就是把上述代码中sort函数的参数变为

std::sort(a.begin(), a.begin() + 4);
std::sort(b, b + 4);

可以看到,只对范围以内的元素进行了升序排列,而范围以外的元素,保持原本的样子不动。
在这里插入图片描述
同样的,当我的范围改为

std::sort(a.begin() + 2, a.begin() + 7);
std::sort(b + 2, b + 7);

结果是:
在这里插入图片描述

3.2 降序排列

有两种方法:
方法1:使用反向迭代器rbegin(),rend()
方法2:使用三个参数的sort函数
此时,需要定义一个返回值类型为bool的函数
1.测试代码

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>

template <class Type>
void printArray(Type Array[], int size){
	for (size_t i = 0; i < size; ++i) {
		std::cout << Array[i] << " ";
	}
	std::cout << std::endl << std::endl;
}

template <class Type>
void printVector(const std::vector<Type> &V) {
	for (auto i : V){
		std::cout << i << " ";
	}
	std::cout << std::endl << std::endl;
}

bool comp_int(int i, int j) {
	return i > j;
}

bool comp_str(std::string i, std::string j) {
	return i > j;
}

int main(int argc, char* argv[]) {

	std::vector<int> a{ 1, 3, 51, 22, 4, 6, 9, 0 };
	std::string b[8] = { "a", "c", "d", "e", "b", "g", "f", "h" };
	
	std::cout << "排序前: " << std::endl;
	std::cout << "a: ";
	printVector(a);
	std::cout << "b: ";
	printArray(b, 8);
	
	std::sort(a.begin(), a.end(), comp_int);
	std::sort(b, b + 8, comp_str);

	std::cout << "排序后: " << std::endl;
	std::cout << "a: ";
	printVector(a);
	std::cout << "b: ";
	printArray(b, 8);

	return 0;
}

2.结果
在这里插入图片描述

3.3 使用sort函数对类对象进行排序

有好几种方法,只重点说明比较简单的几种

  • 方法1:外部比较函数
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>

class TestSort {
public:
	int value_;
	TestSort(int x) :value_(x){ }	// 构造函数
};

void printVector(const std::vector<TestSort> &V) {
	for (auto i : V){
		std::cout << i.value_ << " ";
	}
	std::cout << std::endl << std::endl;
}

bool comp_Test(const TestSort &a, const TestSort &b) {
	return a.value_ > b.value_;
}

int main(int argc, char* argv[]) {

	std::vector<TestSort> Tvec;
	Tvec.push_back(TestSort(3));
	Tvec.push_back(TestSort(1));
	Tvec.push_back(TestSort(6));
	Tvec.push_back(TestSort(5));
	Tvec.push_back(TestSort(4));
	Tvec.push_back(TestSort(2));
	
	std::cout << "排序前: " << std::endl;
	std::cout << "a: ";
	printVector(Tvec);

	std::cout << "-------------↓↓降序排列↓↓-------------" << std::endl;	
	std::sort(Tvec.begin(), Tvec.end(), comp_Test);

	std::cout << "排序后: " << std::endl;
	std::cout << "a: ";
	printVector(Tvec);


	return 0;
}

结果:
在这里插入图片描述

  • 方法2:重载该类的<操作符,使用两参数的sort函数
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>

class TestSort {
public:
	int value_;
	TestSort(int x) :value_(x){ }	// 构造函数

	bool operator<(const TestSort &t) const {
		return value_ < t.value_;
	}
};

void printVector(const std::vector<TestSort> &V) {
	for (auto i : V){
		std::cout << i.value_ << " ";
	}
	std::cout << std::endl << std::endl;
}

int main(int argc, char* argv[]) {

	std::vector<TestSort> Tvec;
	Tvec.push_back(TestSort(3));
	Tvec.push_back(TestSort(1));
	Tvec.push_back(TestSort(6));
	Tvec.push_back(TestSort(5));
	Tvec.push_back(TestSort(4));
	Tvec.push_back(TestSort(2));
	
	std::cout << "排序前: " << std::endl;
	std::cout << "a: ";
	printVector(Tvec);

	std::cout << "-------------↓↓↓↓-------------" << std::endl;	
	std::sort(Tvec.begin(), Tvec.end());

	std::cout << "排序后: " << std::endl;
	std::cout << "a: ";
	printVector(Tvec);


	return 0;
}

结果:
在这里插入图片描述

  • 方法3:声明一个比较类
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>

class TestSort {
public:
	int value_;
	TestSort(int x) : value_(x){ }	// 构造函数
};

class comp {
public:
	bool operator()(const TestSort &a, const TestSort &b) {
		return a.value_ < b.value_;
	}
};

void printVector(const std::vector<TestSort> &V) {
	for (auto i : V){
		std::cout << i.value_ << " ";
	}
	std::cout << std::endl << std::endl;
}

int main(int argc, char* argv[]) {

	std::vector<TestSort> Tvec;
	Tvec.push_back(TestSort(3));
	Tvec.push_back(TestSort(1));
	Tvec.push_back(TestSort(6));
	Tvec.push_back(TestSort(5));
	Tvec.push_back(TestSort(4));
	Tvec.push_back(TestSort(2));
	
	std::cout << "排序前: " << std::endl;
	std::cout << "a: ";
	printVector(Tvec);

	std::cout << "-------------↓↓↓↓-------------" << std::endl;	
	std::sort(Tvec.begin(), Tvec.end(), comp());

	std::cout << "排序后: " << std::endl;
	std::cout << "a: ";
	printVector(Tvec);


	return 0;
}

这里定义了一个类comp,它定义了operator(),因此comp的对象就是函数对象,它的对象可以当作函数一样使用。在comp的operator()中,有两个TestSort类类型的参数,它们进行比较得到布尔值。
sort函数中第三个参数用的是默认构造函数构造的一个comp的临时对象。
假设comp()创建的临时变量是compare,即comp compare,std::sort(Tvec.begin(), Tvec.end(), compare); 由于我们为comp 定义了operator()操作符,而且参数就是两个TestSort类对象,在比较两个TestSort类对象时,会调用compare(a, b)

结果:
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值