结构体排序sort简单 收藏
2018-01-20 21:15
239 查看
#include "stdafx.h"
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
struct student
{
string name;//姓名
int math;//数学成绩
int english;//英语
};
int c =0;//控制排序规则
bool compare(student a, student b)
{
if (c == 0)//按照姓名递增排序
{
return (a.name<=b.name);
}
else if (c == 1)//按照数学成绩递增排序
{
return (a.math <= b.math);
}
else//按照英语成绩递增排序
{
return (a.english <= b.english);
}
}
int _tmain(int argc, _TCHAR* argv[])
{
student array[3];
array[0].name = "John";
array[0].math = 86;
array[0].english = 91;
array[1].name = "Michelle";
array[1].math = 85;
array[1].english =95;
array[2].name = "Kim";
array[2].math = 89;
array[2].english = 60;
cout << "原始顺序:" << endl;
for (int i = 0; i <3; i++)
cout << array[i].name << ' ' << array[i].math << ' ' << array[i].english << endl;
cout << "按姓名递增排序:" << endl;
c =0;
sort(array,array+3,compare);
for (int i = 0; i <3;i++)
cout << array[i].name << ' ' << array[i].math << ' ' << array[i].english << endl;
cout << "按数学成绩递增排序:" << endl;
c =1;
sort(array, array + 3, compare);
for (int i = 0; i <3; i++)
cout << array[i].name << ' ' << array[i].math << ' ' << array[i].english << endl;
system("pause");
return 0;
}
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
struct student
{
string name;//姓名
int math;//数学成绩
int english;//英语
};
int c =0;//控制排序规则
bool compare(student a, student b)
{
if (c == 0)//按照姓名递增排序
{
return (a.name<=b.name);
}
else if (c == 1)//按照数学成绩递增排序
{
return (a.math <= b.math);
}
else//按照英语成绩递增排序
{
return (a.english <= b.english);
}
}
int _tmain(int argc, _TCHAR* argv[])
{
student array[3];
array[0].name = "John";
array[0].math = 86;
array[0].english = 91;
array[1].name = "Michelle";
array[1].math = 85;
array[1].english =95;
array[2].name = "Kim";
array[2].math = 89;
array[2].english = 60;
cout << "原始顺序:" << endl;
for (int i = 0; i <3; i++)
cout << array[i].name << ' ' << array[i].math << ' ' << array[i].english << endl;
cout << "按姓名递增排序:" << endl;
c =0;
sort(array,array+3,compare);
for (int i = 0; i <3;i++)
cout << array[i].name << ' ' << array[i].math << ' ' << array[i].english << endl;
cout << "按数学成绩递增排序:" << endl;
c =1;
sort(array, array + 3, compare);
for (int i = 0; i <3; i++)
cout << array[i].name << ' ' << array[i].math << ' ' << array[i].english << endl;
system("pause");
return 0;
}
相关文章推荐
- 从最简单的vector中sort用法到自定义比较函数comp后对结构体排序的sort算法
- 从最简单的vector中sort用法到自定义比较函数comp后对结构体排序的sort算法
- 网上关于sort结构体排序都不完整,我来写一个完整版的 2014-08-09 16:50 60人阅读 评论(0) 收藏
- 从最简单的vector中sort用法到自定义比较函数comp后对结构体排序的sort算法
- HDU5821 贪心,排序(+sort结构体排序,稳定性和不稳定性)
- 杭电OJ(HDOJ)1862题:EXCEL排序(结构体sort)
- List<T>集合的Sort自定义排序用法简单解析
- c++中sort的vector中结构体排序的两种方法说明
- day_3-acm贪心(sort结构体排序)
- HDU--杭电--3293--sort--结构体排序
- 简单结构体的应用(商品排序C语言)
- hdu 2192 MagicBuilding(简单的水题,sort排序)
- hdoj 1862 EXCEL排序 【结构体+sort】
- 数据结构杂谈(二)简单有趣的地精排序Gnome sort
- EXCEL排序(sort结构体排序)
- 暑假集训 8.16 数据结构实验之排序三:bucket sort (简单的桶排序)
- sort(key= )排序 分类: python基础学习 2013-07-02 16:50 232人阅读 评论(0) 收藏
- BToj 1031问题 B: KACA的数字排序 【大数比较--结构体sort && java大数比较】
- qsort和sort对结构体的排序
- 经典例题 结构体 排序(收藏)