algorithm中sort函数的使用
来源:互联网 发布:wavecom短信猫软件 编辑:程序博客网 时间:2024/05/18 04:59
知识点:
只有定义为vector<int>型变量,才可以使用第一种形式;
第二种形式,需自己定义数组元素比较大小的方式,函数返回类型为bool,调用时,只需函数名,不要加()
sort(a.begin(),a.end)sort(a.begin(),a.end,myfun)
#include<iostream>#include<vector>#include<algorithm>using namespace std;typedef struct{int val;}node;bool great(node a, node b){return a.val < b.val;}bool les(node a, node b){return a.val > b.val;}void print(vector<node> a){for (node t:a){cout << t.val << ' ';}cout << endl;}int main(){vector<node> vec(4);vec[0].val = 3;vec[1].val = 2;vec[2].val = 1;vec[3].val = 5;cout << "初始数据" << endl;print(vec);sort(vec.begin(),vec.end(),great);cout << "升序排列" << endl;print(vec);sort(vec.begin(), vec.end(),les);cout << "降序排列" << endl;print(vec);system("pause");return 0;}
阅读全文
0 0
- algorithm中sort函数的使用
- <algorithm> 中sort的使用
- algorithm中sort函数
- algorithm的sort排序函数
- C++<algorithm>中sort的比较函数写法
- C++<algorithm>中sort的比较函数写法
- C++<algorithm>中sort的比较函数写法
- Algorithm头文件中的sort函数使用
- <algorithm>中的sort()函数
- #include <algorithm>中sort的一般用法
- #include <algorithm>中sort的一般用法
- #include <algorithm>中sort的一般用法
- STL: <algorithm>中 sort()
- STL中sort()函数的使用
- VC中sort()排序函数的使用
- STL:algorithm中排序函数sort(升序排列函数)和reverse(反转排列函数)的简单用法
- algorithm 中常见的函数
- C++标准模板库<algorithm>下的函数sort
- PL/SQL数据设计
- 人工神经网络之激活函数 -tanh函数
- C++实现员工工资管理系统
- MD5加密(java实现)
- oracle多行函数
- algorithm中sort函数的使用
- IDE&接口初步认知
- 记录负数个数平均正数值
- 母函数博客整理
- ThinkPHP总结三
- linux设备驱动模型之总线、设备、驱动三者的关系
- oracle多表查询
- hashmap to json
- linux学习笔记-day2