当前位置:   article > 正文

C++--问题35--min和max函数和minmax函数的用法_c++max函数

c++max函数

C++--问题35--min和max函数和minmax函数的用法


1.max函数

(1)C = max(A, B);返回队列中的最大值。

#define max(a,b)            (((a) > (b)) ? (a) : (b))

2.min函数

(1)C = min(A, B);返回队列中的最小值。

3.minmax函数(C++11

minmax()函数算法标头的库函数,用于查找最小和最大值,它接受两个值并返回一对最小和最大值,该对中的第一个元素包含最小值,并且该对中的第二个元素包含最大值。

以数据对的形式(pair)返回队列中的最大值和最小值

注意:要使用minmax()函数 ,包括<algorithm>头文件,或者简单地使用<bits / stdc ++.h>头文件。

  1. // minmax example
  2. #include <iostream> // std::cout
  3. #include <algorithm> // std::minmax
  4. using namespace std;
  5. //C++ STL program to demonstrate use of
  6. //std::minmax() function
  7. int main()
  8. {
  9. int a = -10;
  10. int b = -20;
  11. //finding pair of smallest and largest numbet
  12. auto result = minmax(a, b);
  13. //printing the smallest and largest values
  14. cout << "smallest number is: " << result.first << endl;
  15. cout << "largest number is: " << result.second << endl;
  16. return 0;
  17. }

结果:

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/107737
推荐阅读
相关标签
  

闽ICP备14008679号