简单的结构体排序

//在网上搜索结构体排序很多都是使用冒泡排序来进行

//学习了lambda表达式后,产生用这个来作为谓词对结构体进行排序

//很简单的结构体排序,结构体只是很简单的string类型+int类型

#include<string>

#include<iostream>

#include<algorithm>

#include<vector>

#include<functional>

#include<numeric>

using namespace std;

//定义结构体

struct test

{

public:

string name;

int score;

test () =default;

};

int main()

{

vector<test> aaa;

test tem;

int n;

//只举了3个例子;

for(n=1;n<=3;++n)

{

cin>>tem.name>>tem.score;

aaa.push_back(tem);

}

//排序部分使用了<algorithm>库的sort函数,使用一个lambda表达式来作为谓词

//如果用函数的话直接写上函数的名字

sort(aaa.begin(),aaa.end(),[=](test a,test b){return a.score<b.score;});

for(auto c:aaa)

cout<<c.name<<":"<<c.score<<endl;

system("pause");

return EXIT_SUCCESS;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值