C++<algorithm>中sort的比较函数写法
来源:互联网 发布:淘宝购物车的东西少了 编辑:程序博客网 时间:2024/06/05 19:47
转自:http://www.wl566.com/biancheng/98907.html
C++<algorithm>中sort的比较函数写法,有需要的朋友可以参考下。
定义排序函数:
方法1:声明外部比较函数
bool Less(const Student& s1, const Student& s2){ return s1.name < s2.name; //从小到大排序}std::sort(sutVector.begin(), stuVector.end(), Less);
注意:比较函数必须写在类外部(全局区域)或声明为静态函数
当comp作为类的成员函数时,默认拥有一个this指针,这样和sort函数所需要使用的排序函数类型不一样。
否则,会出现错误
方法2:重载类的比较运算符
bool operator<(const Student& s1, const Student& s2){ return s1.name < s2.name; //从小到大排序}std::sort(sutVector.begin(), stuVector.end());
方法3:声明比较类
struct Less{ bool operator()(const Student& s1, const Student& s2) { return s1.name < s2.name; //从小到大排序 }};std::sort(sutVector.begin(), stuVector.end(), Less());
0 0
- C++<algorithm>中sort的比较函数写法
- C++<algorithm>中sort的比较函数写法
- C++<algorithm>中sort的比较函数写法
- 浅谈C/C++排序函数中cmp()比较函数的写法(qsort sort函数)
- Java sort 的比较函数写法
- C++ sort的比较函数写法
- algorithm中sort函数
- algorithm中sort函数的使用
- algorithm的sort排序函数
- c++ 中 sort 与 c 的去sort cmp 比较函数 书写区别,待总结
- <algorithm> 中sort的使用
- Lua 中 table.sort 的比较函数
- 浅谈C/C++排序函数中cmp()比较函数的写法
- 浅谈C/C++排序函数中cmp()比较函数的写法
- 浅谈C/C++排序函数中cmp()比较函数的写法
- sort()/qsort()函数的用法及cmp比较函数的写法
- <algorithm>中的sort()函数
- #include <algorithm>中sort的一般用法
- 4.5消费者采购列表---多选项CheckBox的应用
- 【OSS】文件夹相关操作
- 组合数
- 一个好看的样式,datagriview
- 怎样在自定义工具中添加箭头工具
- C++<algorithm>中sort的比较函数写法
- 多线程 线程池 sock IO复用
- G++ 输出高亮
- Android照片墙完整版,完美结合LruCache和DiskLruCache
- php实现web端批量杀死Linux进程和清理内存
- java反射详解
- 百度地图无法定位,即无法调用onReceiveLocation。
- 模糊查询
- Linux中搭建SVN服务器