一、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 vec{3, 5, 1, 4, 2}; auto minIter = min_element(vec.begin(), vec.end()); // minIter指向vec中的數字1
4、同樣的,當我們需要使用自定義的比較函數時,只需要將自定義的比較函數作為參數傳入即可。
vector 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 vec{3, 5, 1, 4, 2}; auto minMaxPair = minmax(vec.begin(), vec.end()); // minMaxPair的值為pair(1, 5)
3、同樣的,當我們需要使用自定義的比較函數時,只需要將自定義的比較函數作為參數傳入即可。
vector 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::min(); // minValue的值為-2147483648 int maxValue = numeric_limits::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 vec{3, 5, 1, 4, 2}; auto minIter = min_element(vec.begin(), vec.end()); // minIter指向vec中的數字1 vector 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; }
原創文章,作者:UGZOG,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/349459.html