c++min函数全面解析

发布时间:2023-05-23

一、min函数的基本用法

1、min函数是C++标准库中的函数,用于返回两个参数中较小的一个。 2、函数的语法如下:min(a, b),其中a、b为两个需要比较大小的参数。

int a = 6, b = 4;
int minNumber = min(a, b);   // minNumber的值为4

3、除了上述用法之外,min函数还支持多个参数的比较,在多个参数中返回最小值。

int a = 6, b = 4, c = 8;
int minNumber = min(a, b, c);   // minNumber的值为4

二、对浮点数进行比较

1、在比较浮点数时,我们需要考虑到精度问题。 2、如果两个浮点数相减的结果非常小,比如10的负10次方,这个时候可能会出现由于精度问题而出现错误的结果。

double a = 1.0000000001, b = 1.0;
double minNumber = min(a, b);   // minNumber的值为1

3、为了避免上述问题,我们可以使用浮点数比较的方法。

double a = 1.0000000001, b = 1.0;
double minNumber = (fabs(a-b)<1e-9)?a:b;   // minNumber的值为1.0000000001

三、使用min函数比较自定义类型的对象

1、当我们需要比较自定义类型的对象时,我们需要重载小于运算符。

class Student {
public:
    string name;
    int age;
    bool operator < (const Student& s) const {
        return age < s.age;
    }
};
Student a{"Tom", 18}, b{"Lucy", 20};
Student minStudent = min(a, b);   // minStudent的值为Tom, 18

四、使用自定义比较函数或Lambda表达式

1、如果我们需要对自定义类型的对象进行比较,可以使用自定义比较函数或Lambda表达式。

bool compareByName(const Student& s1, const Student& s2) {
    return s1.name < s2.name;
}
Student minStudent = min(a, b, compareByName);   // minStudent的值为Lucy, 20

2、Lambda表达式是C++11中新增的一种语法,用于定义匿名函数。

Student minStudent = min(a, b, [](const Student& s1, const Student& s2){ return s1.name < s2.name; });   // minStudent的值为Lucy, 20

五、使用min_element函数返回容器中最小值的迭代器

1、min_element函数是C++标准库中的函数,用于返回容器中最小值的迭代器。 2、函数的语法如下:min_element(first, last, compareFunc),其中first和last分别是容器的起始地址和终止地址,compareFunc是一个自定义的比较函数,用来比较容器元素的大小。 3、需要注意的是,当比较基本数据类型时,不需要指定比较函数。但是,当比较自定义类型的对象时,需要提供自定义的比较函数。

vector<int> vec{3, 5, 1, 4, 2};
auto minIter = min_element(vec.begin(), vec.end());   // minIter指向vec中的数字1

4、同样的,当我们需要使用自定义的比较函数时,只需要将自定义的比较函数作为参数传入即可。

vector<Student> stuVec{{"Tom", 18}, {"Lucy", 20}, {"Lily", 19}};
auto minStuIter = min_element(stuVec.begin(), stuVec.end(), [](const Student& s1, const Student& s2){ return s1.age < s2.age; });   // minStuIter指向年龄最小的Tom

六、使用minmax函数同时返回容器中最小值和最大值

1、minmax函数是C++标准库中的函数,用于返回容器中最小值和最大值。函数返回值是一个pair类型,其中pair.first表示最小值,pair.second表示最大值。 2、函数的语法如下:minmax(first, last, compareFunc),其中first和last分别是容器的起始地址和终止地址,compareFunc是一个自定义的比较函数,用来比较容器元素的大小。

vector<int> vec{3, 5, 1, 4, 2};
auto minMaxPair = minmax(vec.begin(), vec.end());   // minMaxPair的值为pair(1, 5)

3、同样的,当我们需要使用自定义的比较函数时,只需要将自定义的比较函数作为参数传入即可。

vector<Student> stuVec{{"Tom", 18}, {"Lucy", 20}, {"Lily", 19}};
auto minMaxStuPair = minmax(stuVec.begin(), stuVec.end(), [](const Student& s1, const Student& s2){ return s1.age < s2.age; });   // minMaxStuPair的值为pair(Tom, Lucy)

七、使用numeric_limits类来获取最大值和最小值

1、numeric_limits是C++标准库中的类,用于获取各种数值类型的极限值。 2、通过定义numeric_limits对象的min()方法和max()方法,我们可以获取某种数据类型的最小值和最大值。

int minValue = numeric_limits<int>::min();   // minValue的值为-2147483648
int maxValue = numeric_limits<int>::max();   // maxValue的值为2147483647

八、完整代码

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <cmath>
#include <limits>
using namespace std;
class Student {
public:
    string name;
    int age;
    bool operator < (const Student& s) const {
        return age < s.age;
    }
};
bool compareByName(const Student& s1, const Student& s2) {
    return s1.name < s2.name;
}
int main() {
    int a = 6, b = 4;
    int minNumber = min(a, b);   // minNumber的值为4
    double a1 = 1.0000000001, b1 = 1.0;
    double minNumber1 = (fabs(a1-b1)<1e-9)?a1:b1;   // minNumber1的值为1.0000000001
    Student s1{"Tom", 18}, s2{"Lucy", 20};
    Student minStudent1 = min(s1, s2);   // minStudent1的值为Tom, 18
    Student minStudent2 = min(s1, s2, compareByName);   // minStudent2的值为Lucy, 20
    vector<int> vec{3, 5, 1, 4, 2};
    auto minIter = min_element(vec.begin(), vec.end());   // minIter指向vec中的数字1
    vector<Student> stuVec{{"Tom", 18}, {"Lucy", 20}, {"Lily", 19}};
    auto minStuIter = min_element(stuVec.begin(), stuVec.end(), [](const Student& s1, const Student& s2){ return s1.age < s2.age; });   // minStuIter指向年龄最小的Tom
    auto minMaxPair = minmax(vec.begin(), vec.end());   // minMaxPair的值为pair(1, 5)
    auto minMaxStuPair = minmax(stuVec.begin(), stuVec.end(), [](const Student& s1, const Student& s2){ return s1.age < s2.age; });   // minMaxStuPair的值为pair(Tom, Lucy)
    int minValue = numeric_limits<int>::min();   // minValue的值为-2147483648
    int maxValue = numeric_limits<int>::max();   // maxValue的值为2147483647
    return 0;
}