max()、min()、swap()简单介绍
来源:互联网 发布:pcb设计软件 编辑:程序博客网 时间:2024/06/05 09:58
这3个函数是<algorithm>算法程序库中的辅助函数,使用时需包含algorithm头文件。以下是max和min的源代码:
以上是max和min的简单形式,两个函数还存在另一个版本,具体源代码如下:
以下为第二种版本的max和min函数的验证试验,注意默认情况下max和min都会返回第二个元素b,所以在写comp比较函数时,注意当条件为真时返回第二个元素。
#include <iostream>#include <algorithm>using namespace std;struct test{int a;double b;};bool comp(test m,test n){if(m.a<n.a)return true;if(m.a>n.a)return false;}int main(){test smp1={2,8.45652};test smp2={4,1.23454};cout<<"smp1: "<<smp1.a<<" "<<smp1.b<<endl;cout<<"smp2: "<<smp2.a<<" "<<smp2.b<<endl;test smp3=max(smp1,smp2,comp);cout<<"smp3: "<<smp3.a<<" "<<smp3.b<<endl;return 0;}swap函数源代码如下所示:
该函数使用较简单,不再介绍。
1 0
- max()、min()、swap()简单介绍
- 通用工具Utilities(二)----min max swap offsetof
- 产生(min max)的随机数
- 获取随机数(max与min之间)
- std::max与max宏 混淆解决办法(min同样)
- IMSI(MIN)介绍
- std::min, std::max, std::swap, std::rel_ops的简单使用
- mean(求均值),max/min(求最值)
- 存储过程分页(id>(max(id)) id<(min(id)))
- 包含max(min)的栈及队列
- 常量时间的栈操作(min/max)
- 常量时间的队列操作(min/max)
- 最大公约数 和最小公倍数。(max Common Divisor min common multiple)
- PhotoShop算法实现--Max/Min运算(十四)
- Collection.max min最大最小值 集合(list set map)
- 最大最小距离算法(Max-Min-diatance)
- 数学符号arg(arg min/max)含义
- Assert(),min(),max(),pow()(4月11日)
- MyBatis学习笔记(一)——配置文件中的别名以及mapper中的namespace
- 1008
- 中文分词预处理之N最短路径法小结
- Qt 常用类(14)—— QCheckBox
- 1009
- max()、min()、swap()简单介绍
- 1010
- 1011
- Qt 常用类 (15)—— QRadioButton
- 1012
- ftp连不上的原因
- Android中的IPC方式——使用Bundle和文件共享
- 1013
- Qt 常用类 (16)—— QLineEdit