简单的结构体排序
2017-02-11 22:04
113 查看
//在网上搜索结构体排序很多都是使用冒泡排序来进行
//学习了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;
}
//学习了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;
}
相关文章推荐
- 从最简单的vector中sort用法到自定义比较函数comp后对结构体排序的sort算法
- 结构体简单排序-一级-二级
- 简单的结构体二级排序
- hdoj EXCEL排序 1862 (结构体)简单题
- 结构体排序sort简单 收藏
- hdu 1069 Monkey and Banana (结构体排序,也属于简单的dp)
- 从最简单的vector中sort用法到自定义比较函数comp后对结构体排序的sort算法
- 从最简单的vector中sort用法到自定义比较函数comp后对结构体排序的sort算法
- 结构体简单排序练习 题目1014:排名
- 简单结构体的应用(商品排序C语言)
- 三种简单排序的对比与分析(未完待续)
- 重新温习数据结构二:简单排序
- 关于VS2005中GridView的自定义分页,单选、多选、排序、自增列的简单应用
- 简单函数对象取代默认排序准则,改变默认sort()的行为
- 简单选择排序
- 关于VS2005中GridView的自定义分页,单选、多选、排序、自增列的简单应用
- 排序问题的简单实现!
- 关于VS2005中GridView的自定义分页,单选、多选、排序、自增列的简单应用
- 关于GridView中自定义分页、单选、多选、排序、自增列的简单应用
- 关于GridView中自定义分页、单选、多选、排序、自增列的简单应用(转,非常的棒)