这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&l