C++ std::max怎么用?用法详细介绍

2021年4月5日15:55:56 发表评论 1,961 次浏览

本文概述

std::max在头文件<algorithm>中定义, 用于查找传递给它的最大数字。如果有多个, 则返回第一个。可以按以下方式使用它:

  1. 它比较在其参数中传递的两个数字, 并返回两个中较大的一个, 并且如果两者相等, 则返回第一个。
  2. 它也可以使用二元函数, 由用户预先定义, 然后作为参数传递给std::max()。
  3. 如果我们想找到最大元素在给定列表中, 如果列表中存在多个, 则返回第一个。

这三个版本定义如下:

使用" <"比较元素:

语法如下:

template
     
        constexpr const T& max (const T& a, const T& b);
     

Here, a and b are the numbers to be compared.
Returns: Larger of the two values.

CPP

// C++ program to demonstrate the use of std::max
// C++ program to demonstrate the use of std::max
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
     // Comparing ASCII values of a and b
     cout << std::max( 'a' , 'b' ) << "\n" ;
  
     // Returns the first one if both the numbers
     // are same
     cout << std::max(7, 7);
  
return 0;
}

Python3

# Python 3 program to
# demonstrate the use of std::max
  
# Comparing ASCII
# values of a and b
print ( max ( 'a' , 'b' ))
  
# Returns the first
# one if both the numbers
# are same
print ( max ( 7 , 7 ))
  
# This code is contributed by 
# Smitha Dinesh Semwal

输出如下:

b
7

使用预定义功能比较元素:

语法如下:

template
     
      
constexpr const T& max (const T& a, const T& b, Compare comp);
     

Here, a and b are the numbers to be compared.

comp: Binary function that accepts two values of type T as arguments, and returns a value convertible to bool.
The value returned indicates whether the element passed as f
irst argument is considered less than the second.

The function shall not modify any of its arguments.
This can either be a function pointer or a function object.

Returns: Larger of the two values.
// C++ program to demonstrate the use of std::max
#include<iostream>
#include<algorithm>
using namespace std;
  
// Defining the binary function
bool comp( int a, int b)
{
     return (a < b);
}
int main()
{
     int a = 7;
     int b = 28;
      
     cout << std::max(a, b, comp) << "\n" ;
  
     // Returns the first one if both the numbers
     // are same
     cout << std::max(7, 7, comp);
  
return 0;
}

输出如下:

28
7

在列表中查找最大元素:

语法如下:

template
     
       
constexpr T max (initializer_list
      
        il, Compare comp);
      
     

Here, comp is optional and can be skipped.
il: An initializer_list object.
Returns: Largest of all the values.
// C++ program to demonstrate the use of std::max
#include<iostream>
#include<algorithm>
using namespace std;
  
// Defining the binary function
bool comp( int a, int b)
{
     return (a < b);
}
int main()
{
  
     // Finding the largest of all the numbers
     cout << std::max({1, 2, 3, 4, 5, 10, -1, 7}, comp) << "\n" ;
  
return 0;
}

输出如下:

10

相关文章:

  • std::max_element
  • std::min
  • std::equal
  • C ++中的std::min_element

如果发现任何不正确的地方, 或者想分享有关上述主题的更多信息, 请写评论。

被认为是行业中最受欢迎的技能之一, 我们拥有自己的编码基础C ++ STL通过激烈的问题解决过程来训练和掌握这些概念。

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: