【C++】sort自定义排序示例

sort对于内置类型类型是升序排序,若要进行自定义排序有四种方法实现:

  • STL里的仿函数类。std::greater和std::less,他们在头文件<functional>里,能对内置类型进行排序
  • 自定义排序函数
  • 仿函数类
  • 重载operator<函数

sort用法

下面给出四种方法实现自定义排序的demo:

// sort algorithm example
#include <iostream>     // std::cout
#include <algorithm>    // std::sort
#include <vector>       // std::vector
#include <functional>   // std::greater, std::less
#include <string>   // std::string

#define PRINT_VEC(x) cout << #x": "; for (auto &c : x) cout << c << ' '; cout << endl;

using namespace std;

// 自定义升序排序函数
bool my_less_f(int i, int j) {
    
     return (i<j); }

// 升序排序仿函数类
struct my_less_class {
    
    
	bool operator() (const int &i, const int &j) const {
    
     return (i<j); }
} my_less_object; // my_less_class是类型名,my_less_object为该类型的一个变量。

// 自定义类中,重载operator<函数
struct my_class {
    
    
	my_class() : val(0), name("") {
    
    }
	my_class(int val, string name) : val(val), name(name) {
    
    }
	// 注意,const不要少,不然在priority_queue中会出错
	bool operator< (const my_class &j) const {
    
     return this->val < j.val; } // less,升序排序
	int val;
	string name;
};
int main() {
    
    
	std::vector<int> myvector{
    
     32,71,12,45,26,80,53,33 };   // 32 71 12 45 26 80 53 33

	// sort第三个参数默认用的是less<>()
	// 注意,less<int>和greater<int>都是仿函数类的类型名,不是函数名,因此需要加()构造一个实例
	std::sort(myvector.begin(), myvector.begin() + 4, less<int>());		//(12 32 45 71)26 80 53 33
	std::sort(myvector.begin(), myvector.begin() + 4, greater<int>());  //(71 45 32 12)26 80 53 33
	PRINT_VEC(myvector);

	// 用比较函数,传函数指针
	std::sort(myvector.begin() + 4, myvector.end(), my_less_f); // 12 32 45 71(26 33 53 80)

	// 用仿函数类,注意my_less_class是类型名,需要加()构造一个实例,而my_less_object已经是一个实例
	// 仿函数(functor),就是使一个类的使用看上去象一个函数。其实现就是类中实现一个operator(),这个类就有了类似函数的行为,就是一个仿函数类了。
	std::sort(myvector.begin(), myvector.end(), my_less_object);     //(12 26 32 33 45 53 71 80)
	std::sort(myvector.begin(), myvector.end(), my_less_class());     //(12 26 32 33 45 53 71 80)

	// print out content:
	PRINT_VEC(myvector);

	// 自定义类型排序,自定义类型中,默认调用该类型中的operator<函数排序
	std::vector<my_class> myvector2{
    
     {
    
     32, "a" },{
    
     71, "b" },{
    
     12, "c" }};   // 32 71 12 
	std::sort(myvector2.begin(), myvector2.end()); // 12 32 71
	cout << "myvector2: "; for (auto &c : myvector2) cout << c.val << ',' << c.name << ' '; cout << endl;
	
	// 排序c数组,sort参数也可以只指针,不一定要迭代器
	int myvector3[] = {
    
     32,71,12,45,26,80,53,33 };   // 32 71 12 45 26 80 53 33
	std::sort(myvector3, myvector3+8); // 给地址范围而不是迭代器排序,(12 26 32 33 45 53 71 80)
	PRINT_VEC(myvector3);
	
	// 使用greater和less对pair排序
	vector<pair<int, int>> myvector4{
    
     {
    
     1,2 }, {
    
    4,2}, {
    
    2,1}, {
    
    1,1}, {
    
    1,3} };
	std::sort(myvector4.begin(), myvector4.end());	//  1,1 1,2 1,3 2,1 4,2 ,pair里重载了operator<,默认比较first,first相同则比较second,实现升序排序
	std::sort(myvector4.begin(), myvector4.end(), less<pair<int, int>>()); //  1,1 1,2 1,3 2,1 4,2 , less 实现功能与pair重载的operator功能相同
	std::sort(myvector4.begin(), myvector4.end(), greater<>()); // 4,2 2,1 1,3 1,2 1,1 ,降序排序
	cout << "myvector4: "; for (auto &c : myvector4) cout << c.first << ',' << c.second << ' '; cout << endl;
	return 0;
}

相关/参考链接

猜你喜欢

转载自blog.csdn.net/a435262767/article/details/105361721