李优秀 发表于 2024-7-19 21:38:41

C++初学者指南-5.尺度库(第一部分)--尺度库最小/最大算法

C++初学者指南-5.尺度库(第一部分)–尺度库min/max算法



C++尺度库算法是一块新领域?⇒简短介绍
min

   min(a, b) → a 假如 a < b则返回a,否则返回b
min(a, b, cmp(o,o)→bool) → 假如 cmp(a,b) 为真则返回a, 否则返回b
cppreference
int const a = 2;
int const b = 9;
int x = std::min(a,b);// int x = 2
struct P { int q; char c; };
P pmin = std::min(P{1,'y'}, P{2,'x'},
[](P p1, P p2){ return p1.q < p2.q; });// P pmin {1,'y'};
运行示例代码
    min({v1,v2,v3,…}) → 求最小值 (C++11)
min({v1,v2,v3,…}, cmp(o,o)→bool) → 求最小值
第二个版本使用cmp来比力元素
当第一个版本使用 operatior < 来进行比力时,
所有输入列表{…}元素范例必须一致
cppreference
int const a = 2;
int const b = 9;
int x = std::min({3,4,b,3,a,8});// int x = 2
运行示例代码
    ranges::min(range) → 求最小值 (C++20)
ranges::min(range, cmp(o,o)→bool) → 求最小值
返回范围内最小元素的常量引用
第二个版本使用 cmp 来比力元素,而第一个版本使用 operator <
cppreference
std::vector<int> v {7,9,3,5,3,1,4,8};
auto x = std::ranges::min(v);// int x = 1
struct P { int q; char c; };
std::vector<P> const w {P{3,'a'},P{1,'c'},P{2,'b'}};
auto pmin = std::ranges::min(w,
[](P const& p1, P const& p2){ return p1.q < p2.q; });// P pmin {1,'c'}
运行示例代码
max

   max(a, b) → 假如 a < b为假则返回a,否则返回b
max(a, b, cmp(o,o)→bool) → 假如 cmp(a,b) 为假则返回a,否则返回b
cppreference
int const a = 2;
int const b = 9;
int x = std::max(a,b);// int x = 9
struct P { int q; char c; };
P pmax = std::max(P{1,'y'}, P{2,'x'},
[](P p1, P p2){ return p1.q < p2.q; });// P pmax {2,'x'};
运行示例代码
    max({v1,v2,v3,…}) → 求最大值 (C++11)
max({v1,v2,v3,…}, cmp(o,o)→bool) → 求最大值
第二个版本使用 cmp 来比力元素
当第一个版本使用operator <来比力元素时:
所有输入列表{…}元素范例必须一致
cppreference
int const a = 2;
int const b = 9;
int x = std::max({3,4,b,3,a,8});// int x = 9
运行示例代码
    ranges::max(range) → 求最大值 (C++20)
ranges::max(range, cmp(o,o)→bool) → 求最大值
返回范围内最大元素的常量引用
第二个版本使用cmp用于比力元素,而第一个版本使用operator <
cppreference
std::vector<int> v {7,9,3,5,3,1,4,8};
auto x = std::ranges::max(v);// int x = 9
struct P { int q; char c; };
std::vector<P> const w {P{1,'c'},P{3,'a'},P{2,'b'}};
auto pmax = std::ranges::max(w,
[](P p1, P p2){ return p1.q < p2.q; });// P pmax {3,'a'}
运行示例代码
minmax

   minmax(a, b) → {最小值, 最大值}  (C++11)
minmax(a, b, cmp(o,o)→bool) → {最小值,最大值}
假如a的排序在b之前,比力函数/对象cmp(a,b)必须返回true
cppreference
int a = 2;
int b = 9;
auto p = std::minmax(a,b);// std::pair<int,int> p {2,9}
auto min = p.first;// int min = 2
auto max = p.second; // int max = 9
auto = std::minmax(a,b);// int lo = 2, hi = 9C++17
运行示例代码
    minmax({v1,v2,v3,…}) → {最小值,最大值}  (C++11)
minmax({v1,v2,v3,…}, cmp(o,o)→bool) → {最小值,最大值}
第二个版本使用 cmp 来比力元素,
当第一个版本使用 operator < 比力元素时,
所有输入列表{…}元素范例必须一致
cppreference
int const a = 2;
int const b = 9;
auto p = std::minmax({3,0,b,3,a,8});// std::pair<int,int> p {0,9}
auto min = p.first;// int min = 0
auto max = p.second; // int max = 9
auto = std::minmax({3,0,b,3,a,8});// int lo = 0, hi = 9C++17
运行示例代码
    ranges::minmax(range) → {最小值,最大值} (C++20)
ranges::minmax(range, cmp(o,o)→bool) → {最小值,最大值}
返回一对范围内的最小和最大元素的常量引用; 第2版使用cmp来比力元素,而第1版使用 operator <
cppreference
std::vector<int> v {7,9,3,5,3,1,4,8};
auto p = std::ranges::minmax(v);// std::pair<int,int> p {1,9}
auto = std::ranges::minmax(v);
struct P { int q; char c; };
std::vector<P> const w {P{3,'a'},P{2,'b'},P{1,'c'}};
auto = std::ranges::minmax(w,
[](P p1, P p2){ return p1.q < p2.q; });// P lo {1,'c'}, hi {3,'a'}
运行示例代码
clamp (C++17)

   clamp(value, lo, hi) → 返回限定值
clamp(value, lo, hi, cmp(o,o)→bool) → 返回限定值
限定值在lo和hi之间
第二个版本使用 cmp 来比力值,而不是用 operator <
cppreference
int a = std::clamp( 8,1, 5);// int a =5
int b = std::clamp(-4,1, 5);// int b =1
int c = std::clamp(-4, -2, 5);// int c = -2
运行示例代码
min_element

   https://i-blog.csdnimg.cn/direct/ab1f72c1f8bd4bec9808dd3ec93713ae.png#pic_center
第二个版本使用“compare”来比力元素,
而第一个版本使用 operator <
cppreference
https://i-blog.csdnimg.cn/direct/f73ec3bc4d14474f9d5e3daccddee5e2.png#pic_center
运行示例代码
    https://i-blog.csdnimg.cn/direct/6d9c3a0e673e4c12972012e8dbf02847.png#pic_center
使用operator < 来比力元素;或者可以作为第二个参数传递自界说的函数对象 comp来比力元素
cppreference
std::vector<int> v {7,9,3,5,3,2,4,1,8,0};
auto i = std::ranges::min_element(v);
auto min = *i;// int min = 0
运行示例代码
max_element

   https://i-blog.csdnimg.cn/direct/f8232e8da7ab4cc29c0f74b444794f01.png#pic_center
   第二个版本使用“compare”来比力元素,
而第一个版本使用 operator <
cppreference
https://i-blog.csdnimg.cn/direct/7f8f1787f36143c0a363d4fe01aae41f.png#pic_center
运行示例步伐
      https://i-blog.csdnimg.cn/direct/07181a0ff3c041009aaad356e846a63e.png#pic_center
使用operator < 来比力元素;或者可以作为第二个参数传递自界说的函数对象 comp来比力元素
cppreference
std::vector<int> v {7,9,3,5,3,2,4,1,8,0};
auto i = std::ranges::max_element(v);
auto max = *i;// int max = 9
运行示例代码
minmax_element

   https://i-blog.csdnimg.cn/direct/ad3335f101574628ae7118732b0b07eb.png#pic_center
返回一个指向输入范围中最小和最大元素的迭代器对std::pair;
第二个版本使用 comp 来比力元素,
而第一个版本使用 operator <
cppreference
https://i-blog.csdnimg.cn/direct/04de8c2759c4436290470ebd8995a67f.png#pic_center
运行示例代码
    https://i-blog.csdnimg.cn/direct/5443ed34e2a74776a7a6ad495c264eb6.png#pic_center
返回一个std::pair,其中包含输入范围中最小和最大元素的迭代器;
使用operator < 来比力元素;或者作为第二个参数传递一个自界说函数(对象)cmp
cppreference
std::vector<int> v {7,1,3,5,3,8,6,2,9,0};
auto = std::ranges::minmax_element(v);
auto min = *i;// int min = 0
auto max = *j;// int max = 9
运行示例代码
相关内容

视频:最大最小算法 by Conor Hoekstra
尺度算法概述
C++尺度库算法介绍
尺度序列容器(vector、deque、list、…)
尺度关联容器(map、set、…)
尺度序列视图
cppreference:算法库
cppreference:容器库
视频:什么是 C++ 尺度库?
视频:一小时内掌握 105 个 STL 算法 (Jonathan Boccara,2018)
C++ 之旅:容器和算法
算法概述表:
https://i-blog.csdnimg.cn/direct/da074b30e937466fade87be92c877223.png#pic_center
附上原文链接
假如文章对您有用,请顺手点个赞,谢谢!^_^

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: C++初学者指南-5.尺度库(第一部分)--尺度库最小/最大算法