C++ STL 中的 std::minmax() 和 std::minmax_element()

2024 年 8 月 28 日 | 3 分钟阅读

我们已经知道,C++ 编程语言拥有一个如海洋般浩瀚的标准模板库(Standard Template Library),其中包含了许多预定义的功能。为了找出由用户输入或由测试/开发人员预定义的数组元素中的最大值或最小值,我们使用标准的最小函数或整个函数。

为了同时获取最大值和最小值,可能会需要一些重复编写或反复使用代码。因此,C++ STL 以 std minmax() 和 minmax_element() 函数的形式为我们提供了解决方案。这两个函数都接受参数,形式可以是任何自然数、整数的变量,也可以是元素数组。

minmax() 函数的 C++ 代码

输出

/ tmp / 1Lj mBg VyK h.o
The minimum value is : 33
The maximum value is : 63
minimum value : 11
maximum value  : 16

minmax_element() 函数的 C++ 代码

输出

/ tmp /1 LjmB gVy Kh.o
position of the minimum value obtained after operating is : 7
position of the maximum value received after working is : 10

position of the minimum value obtained after operating is : 1
position of the maximum value received after working is : 5