使用algorithm中的sort函数对数据进行排序

本文介绍如何在C++中实现自定义类型的排序,并通过示例展示了不同排序标准的应用,包括按照ID、姓名及成绩等多种排序方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

// 使用sort()函数对自定义类型排序.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<algorithm>
#include<string>
#include<iostream>
#include<functional>
using namespace std;
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<list>
#include<stack>
class Student
{
private:
	int m_id;
	string m_name;
	int m_math;
	int m_english;
public:
	Student(int id, const string &name, int math, int english)
	{
		m_id = id;
		m_name = name;
		m_math = math;
		m_english = english;
	}
	int GetId() { return m_id; }
	string GetName() { return m_name; }
	int GetMath() { return m_math; }
	int GetEnglish() { return m_english; }
};
//将默认的 less<基本类型>  也就是 < 号进行重载,这样在使用sort排序的时候
//才会知道要按什么来比较,直接使用自定义类型基本类型太多,不知道要按那一个比较就会报错了
bool operator<(Student &stu1, Student &stu2)			//只能重载小于号 < ,因为sort()排序函数默认的就是<号
{
	return stu1.GetId()<stu2.GetId();
}
bool name_sort_less(Student& stu1, Student &stu2)		//第二种排序方式
{
	return stu1.GetName() < stu2.GetName();
}

bool name_sort_greater(Student& stu1, Student &stu2)
{
	return stu1.GetName() > stu2.GetName();
}

bool math_greater_english_greater(Student &stu1, Student &stu2)
{
	if (stu1.GetMath() != stu2.GetMath())
		return stu1.GetMath() < stu2.GetMath();			//如果数学成绩不相等,那么按照数学成绩升序排序	
	return stu1.GetEnglish() < stu2.GetEnglish();	//如果数学成绩相等,按照英语成绩升序排列
}
void InitVector(vector<Student> &myVect)
{
	Student Hong(1001, "HongHong", 100, 120);
	Student Ming(1004, "MingMing", 100, 101);
	Student Gang(1002, "GangGang", 89, 76);
	Student Hongey(1006, "Honey\t", 90, 108);
	myVect.push_back(Hong);
	myVect.push_back(Ming);
	myVect.push_back(Gang);
	myVect.push_back(Hongey);
}
void  Print(Student &stu)
{
	cout << stu.GetId() << "\t" << stu.GetName() << "\t" << stu.GetMath() << "\t" << stu.GetEnglish() << endl;
}
void PrintNum(int val)
{
	cout << val << "\t";
}
int main()
{
	vector<Student> myVect;
	InitVector(myVect);
	cout << "原来的数据" << endl;
	for_each(myVect.begin(), myVect.end(), Print);

	sort(myVect.begin(), myVect.end());				//使用重载的小于号进行排序
	cout << endl << "按照Id进行排序后输出" << endl;
	for_each(myVect.begin(), myVect.end(), Print);

	sort(myVect.begin(), myVect.end(), name_sort_less);	//加入了自定义的排序准则
	cout << endl << "按照姓名升序进行排序后输出" << endl;
	for_each(myVect.begin(), myVect.end(), Print);


	sort(myVect.begin(), myVect.end(), name_sort_greater);	//加入了自定义的排序准则
	cout << endl << "按照姓名降序进行排序后输出" << endl;
	for_each(myVect.begin(), myVect.end(), Print);

	cout << endl << "如果数学成绩不相等,那么按照数学成绩升序排序否则按照英语升序排列" << endl;
	sort(myVect.begin(), myVect.end(), math_greater_english_greater);
	for_each(myVect.begin(), myVect.end(), Print);



	set<int> mySet;
	mySet.insert(3);		//set<>使用insert的方式初始化数据
	for_each(mySet.begin(), mySet.end(), PrintNum);		//只能使用mySet.begin()的方式输出数据


	map<int, string>  myMap;
	myMap.insert(pair<int, string>(1, "中国"));
	myMap[2] = "河南";
	myMap.insert(make_pair<int, string>(3, "民权"));

	list<int> myList;
	myList.push_back(10);
	myList.push_front(7);
	myList.pop_back();
	myList.pop_front();
	list<int>::iterator myIter;		//链表可以使用迭代器


	stack<int> myStack;				//栈就不可以使用迭代器操作了
	myStack.push(3);
	myStack.push(5);
	myStack.pop();
	myStack.top();




	system("pause");
	return 0;
}


/*
在sort()函数中没有办法使用greater<>排序模式
在map和set中可以使用  map<int,string,greater<int>>	这样的方式就是按照键first降序排列了
set<int,greater<int>>	将集合按照降序排列


vector<int> 初始化使用 数组的方式或者
*/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值