c++11版本list中sort用lambda表达式
2015-07-09 15:47
459 查看
//定义学生类
class Student
{
public:
string StuName; //姓名
string StuNum; //学号
string Sex; //性别
//成绩
float English; //英语
float SQL; //SQL
float Prob; //概率论
float Circuit; //电路
float CSharp; //C#
float Average; //平均成绩
};
list studList;
c++ 11,可以用lambda表达式
//用平均成绩排序
list.sort( -> bool
{
return a.Average > b.Average;
});
//用c#成绩排序
list.sort( -> bool
{
return a.Average > b.Average;
});
如果是c++11以下版本
1. 函数
bool compare_avaerage (const Student& first, const Student& second)
{
return a.Average > b.Average;
}
然后调用list.sort(compare_average);
2. Function class
class StudentSorter{
public:
operator() (const Student& s1, const Student& s2)
{
// get the field to sort by and make the comparison
return a.Average > b.Average;
}
}
然后你可以调用
StudentSorter sorter;
sort(list.begin(), list.end(), sorter);
class Student
{
public:
string StuName; //姓名
string StuNum; //学号
string Sex; //性别
//成绩
float English; //英语
float SQL; //SQL
float Prob; //概率论
float Circuit; //电路
float CSharp; //C#
float Average; //平均成绩
};
list studList;
c++ 11,可以用lambda表达式
//用平均成绩排序
list.sort( -> bool
{
return a.Average > b.Average;
});
//用c#成绩排序
list.sort( -> bool
{
return a.Average > b.Average;
});
如果是c++11以下版本
1. 函数
bool compare_avaerage (const Student& first, const Student& second)
{
return a.Average > b.Average;
}
然后调用list.sort(compare_average);
2. Function class
class StudentSorter{
public:
operator() (const Student& s1, const Student& s2)
{
// get the field to sort by and make the comparison
return a.Average > b.Average;
}
}
然后你可以调用
StudentSorter sorter;
sort(list.begin(), list.end(), sorter);
相关文章推荐
- 数组中出现次数超过数组长度一半的值 分类: C/C++ 2015-07-09 15:38 142人阅读 评论(0) 收藏
- C++关键字大总结
- 学堂在线c++进阶编程题 11-2
- for、while循环及其变体
- C++面试题一大波
- C++ log
- C语言qsort
- c++使用ado连接 sqlserver使用注意点
- C++ 线程池
- C++ 字符函数库cctype
- VC++ 句柄要及时关闭否则经常性调用句柄会持续增加
- 【C语言 C++】简单关键字Register,Const,Static,Volatile,typedef,Define的理解
- C++ 逻辑表达式&& || !
- C++中的成员对象
- VC++ combo box控件的使用
- 【C语言】求数值的正数次方
- 【C语言】打印1到最大的n位数
- C/C++文件输入输出操作——FILE*、fstream、windowsAPI
- 研究了一下关于c/c++编译器关于导出函数的不同处理
- c语言 DEBUG 日志打印