c++的sort排序(包括结构体)

sort排序

(一)、从小到大:sort()

int arr[]={2,3,4,52,1}
sort(arr,arr+5);

其中括号里面有两个部分,表示从第一个元素arr[0]到第五个元素arr[4]按从小到大的顺序排列

若写为:

sort(arr+i,arr+j)

表示被排序的是arr[i]到arr[j-1],其他元素保持原位置

(二)、从大到小:** greater<数据类型>()**

sort(arr,arr+5,greater<int>());

(三)、自定义一种从大到小排序的方法:cmp

bool cmp(int x,int y)
{
	return x>y;
}
int main(){
	int arr[10];
	for(int i=0;i<10;i++)
	{
		cin>>arr[i];
	}
	sort(arr,arr+10,cmp);
	//如果cmp返回结果为假, 那么函数就会将他们互换位置;
	//如果cmp返回结果为真,就会保持原来位置不变。
	
	//此时返回值为假
}

例题:
题意:有N个正整数,均小于10000,现在要将这些正整数按照除以3的余数从小到大排序,即除以3余0的数排在余1的数前面,余1在余2前面,如果余数相同,则按找正整数的值从小到大排序

输入格式:
第一行:N,0<N<101;
第二行:N个正整数,用空格隔开

#include<bits/stdc++.h>
using namespace std;
int num[105];//在全局定义数组 
bool cmp(int x,int y)
{
	if(x%3==y%3)//余数相同时 
	{
		return x<y;
	}
	else//余数不同时 
	{
		return x%3 <y%3;
	}
}

int main()
{
	int N;
	cin>>N;
	for (int i=0;i<N;i++)
	{
		cin>>num[i];
	}
	sort(num,num+N,cmp);
	//输出 
		for (int i=0;i<N;i++)
	{
		//末尾数不输出空格 
		if(i!=N-1)
		{
			cout<<num[i]<<' ';	
		}
		else
		{
			cout<<num[i]<<endl;
		 } 
		
	}
	return 0;
}

(三)、sort对结构体进行排序

1、首先初始化结构体,即对结构体赋值
原来的做法是一个一个赋值:如下

Student stu;
int score_input;
string name_input;
cin>>score_input>>name_input;
stu.name=name_input;
stu.score=score_input;

可以看到,以上手动给结构体赋值的很麻烦的

改进,用构造函数法,在结构体内进行赋值

struct Student{
	int score;
	string name;
	Student(string n,int s)
	{
		name=n;
		score=s;
	}
};

注意
1、函数名要和结构体名完全相同
2、不能定义返回值类型,也不能有return语句
3、有无形参都🆗,圆括号()内可以为空
4、,当构造函数的花括号里面没有参数,是空的时候称为默认构造函数,只能出现一个默认构造函数
5、竞赛中一般是采用初始化列表的方式:
Student (string n, int s):name(n),score(s){}
其中,花括号不能省略
不需要手动调用,当创建结构体的时候,构造函数会自动被调用

例子如下:

#include<bits/stdc++.h>
using namespace std;
struct Student
{
	int score;
	string name;
	//不要忘了默认构造函数,并且一下两行都不用分号; 
	Student(){} 
	Student(string n,int s):name(n),score(s){}
 } ;
 
 int main()
 {
 	Student stu[3];
 	for(int i=0;i<3;i++)
 	{
 		int s;
 		string n;
 		cin>>n>>s; //字符串也可以cin>>来输出 
 		stu[i]=Student(n,s);
	 }
	 for(int i=0;i<3;i++)
	 {
	 	cout<<stu[i].name<<' '<<stu[i].score<<endl;
	 }
	 
 }

在这里插入图片描述

接下来按姓名来排序,三名同学,四门成绩

#include<bits/stdc++.h>
using namespace std;
struct Student
{
	int score[4];
	string name;
 } ;
 //按照名字排序,从小到大, 
 bool cmp(Student x,Student y){
 	return x.name<y.name;
 } 
 int main()
 {
 	Student stu[3];
 	for(int i=0;i<3;i++)
 	{
 		cin>>stu[i].name;
 		for(int j=0;j<4;j++)
 		{
 			cin>>stu[i].score[j];
		}
	 }
	 sort(stu,stu+3,cmp);
	 for(int i=0;i<3;i++)
	 {
	 	cout<<stu[i].name<<':';
	 	for(int j=0;j<4;j++)
	 	{
	 		cout<<stu[i].score[j]<<' ';
		 }
		 cout<<endl; 
	 }
	 return 0;
 }

在这里插入图片描述

接下来按每门成绩从大到小排序

#include<bits/stdc++.h>
using namespace std;
struct Student
{
	int score[4];
	string name;
 } ; 
 bool cmp(Student x,Student y){
 	//这里不用else是因为一进来就return了! 
 	if(x.score[0]!=y.score[0])
	 {
	 	return x.score[0]>y.score[0];
	  } 
	   	if(x.score[1]!=y.score[1])
	 {
	 	return x.score[1]>y.score[1];
	  } 
	   	if(x.score[2]!=y.score[2])
	 {
	 	return x.score[2]>y.score[2];
	  } 
	   	if(x.score[3]!=y.score[3])
	 {
	 	return x.score[3]>y.score[3];
	  } 
 } 
 int main()
 {
 	Student stu[3];
 	for(int i=0;i<3;i++)
 	{
 		cin>>stu[i].name;
 		for(int j=0;j<4;j++)
 		{
 			cin>>stu[i].score[j];
		}
	 }
	 sort(stu,stu+3,cmp);
	 for(int i=0;i<3;i++)
	 {
	 	cout<<stu[i].name<<':';
	 	for(int j=0;j<4;j++)
	 	{
	 		cout<<stu[i].score[j]<<' ';
		 }
		 cout<<endl; 
	 }
	 return 0;
 }

在这里插入图片描述

接下来将两种方式整合

#include<bits/stdc++.h>
using namespace std;
struct Student
{
	int score[4];
	string name;
 } ; 
 //按照比较名字的方法排序
 bool cmp_name(Student x,Student y)
 {
 	return x.name<y.name;
 }
 bool cmp_score(Student x,Student y){
 	//这里不用else是因为一进来就return了! 
 	if(x.score[0]!=y.score[0])
	 {
	 	return x.score[0]>y.score[0];
	  } 
	   	if(x.score[1]!=y.score[1])
	 {
	 	return x.score[1]>y.score[1];
	  } 
	   	if(x.score[2]!=y.score[2])
	 {
	 	return x.score[2]>y.score[2];
	  } 
	   	if(x.score[3]!=y.score[3])
	 {
	 	return x.score[3]>y.score[3];
	  } 
 } 
 int main()
 {
 	Student stu[3];
 	for(int i=0;i<3;i++)
 	{
 		cin>>stu[i].name;
 		for(int j=0;j<4;j++)
 		{
 			cin>>stu[i].score[j];
		}
	 }
	 //name
	 sort(stu,stu+3,cmp_name);
	 for(int i=0;i<3;i++)
	 {
	 	cout<<stu[i].name<<':';
	 	for(int j=0;j<4;j++)
	 	{
	 		cout<<stu[i].score[j]<<' ';
		 }
		 cout<<endl; 
	 }
	 
	 
	//score
	sort(stu,stu+3,cmp_score);
	 for(int i=0;i<3;i++)
	 {
	 	cout<<stu[i].name<<':';
	 	for(int j=0;j<4;j++)
	 	{
	 		cout<<stu[i].score[j]<<' ';
		 }
		 cout<<endl; 
	 }
	 return 0;
 }


在这里插入图片描述

### C++ 中对结构体进行排序的方法 #### 使用 `std::sort` 对结构体排序 为了实现对自定义结构体类型的排序,可以采用两种主要方法: - **重载 `<` 运算符** 当希望基于某个特定字段(如年龄)来进行默认排序时,在结构体内重载小于号运算符是一种常见做法。这使得可以直接利用标准库中的 `std::sort()` 函数而无需额外提供比较器。 ```cpp #include <iostream> #include <algorithm> #include <vector> struct Person { std::string name; int age; bool operator<(const Person& other) const { // 将此函数声明为const成员函数[^3] return age < other.age; } }; int main() { std::vector<Person> people = {{"Alice", 25}, {"Bob", 20}, {"Charlie", 30}}; std::sort(people.begin(), people.end()); for (auto&& person : people) { std::cout << "Name: " << person.name << ", Age: " << person.age << '\n'; } return 0; } ``` - **传递自定义比较函数** 如果想要根据不同的条件灵活调整排序逻辑,则可以通过向 `std::sort()` 提供第三个参数作为定制化的比较谓词来达成目的。这种方式允许更复杂的排序规则而不必改变原始数据结构的设计。 ```cpp bool compare_by_name(const Person &a, const Person &b) { return a.name < b.name; } // 或者使用lambda表达式简化写法 auto lambda_compare_by_name = [](const Person &a, const Person &b){ return a.name < b.name; }; int main(){ std::vector<Person> people = { /* 初始化列表 */ }; // 使用命名函数或Lambda表达式作为第三参数 std::sort(people.begin(), people.end(), compare_by_name); // Lambda版本 // std::sort(people.begin(), people.end(), lambda_compare_by_name); // 输出结果... } ``` 对于更加复杂的数据类型比如题目提到的学生信息表单(包含姓名、性别等多个属性),同样适用上述原则;只需确保所选的关键字能够唯一区分各个记录即可[^1]。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值