当前位置:   article > 正文

c++ max 比较最大值_c++max

c++max

对于比较大小的方法,我们可以使用 algorithm下的函数,而不需要我们自己写函数。

1. 使用方法

  1. #include <algorithm>
  2. using namespace std;
  3. int a = 6, b = -7;
  4. // Return the integer with the larger absolute value
  5. const int& result1 = max(a, b, abs_greater);
  6. // Return the larger integer
  7. const int& result2 = max(a, b);
  8. cout << "Using integers 6 and -7..." << endl;
  9. cout << "The integer with the greater absolute value is: "
  10. << result1 << "." << endl;
  11. cout << "The integer with the greater value is: "
  12. << result2 << "." << endl;
  13. cout << endl;
  14. // Comparing the members of an initializer_list
  15. const int& result3 = max({ a, b });
  16. const int& result4 = max({ a, b }, abs_greater);
  17. cout << "Comparing the members of an initializer_list..." << endl;
  18. cout << "The member with the greater value is: " << result3 << endl;
  19. cout << "The integer with the greater absolute value is: " << result4 << endl;

2. 运行结果

Using integers 6 and -7...
The integer with the greater absolute value is: -7
The integer with the greater value is: 6.

Comparing the members of an initializer_list...
The member with the greater value is: 6
The integer wiht the greater absolute value is: -7

3. 详细解释


  1. template<class Type>
  2. const Type& max(
  3. const Type& _Left,
  4. const Type& _Right
  5. );
  6. template<class Type, class Pr>
  7. const Type& max(
  8. const Type& _Left,
  9. const Type& _Right,
  10. BinaryPredicate _Comp
  11. );
  12. _Left
  13. The first of the two objects being compared.
  14. _Right
  15. The second of the two objects being compared.
  16. _Comp
  17. A binary predicate used to compare the two objects.
  18. _IList
  19. The initializer list that contains the objects to be compared.
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/107707
推荐阅读
相关标签
  

闽ICP备14008679号