std :: max()函数

 max() function is a library function of algorithm header, it is used to find the largest value from given two values, it accepts two values and returns the largest value and if both the values are the same it returns the first value.

max()函数algorithm标头的库函数,用于从给定的两个值中查找最大值,它接受两个值并返回最大值,如果两个值相同,则返回第一个值。

Note:To use max() function – include <algorithm> header or you can simple use <bits/stdc++.h> header file.

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

1.Syntax of std::max() function

std :: max()函数的语法

 std::max(const T& a, const T& b);

Parameter(s): const T& a, const T& b – values to be compared.

参数: const T&a,const T&b –要比较的值。

Return value: T – it returns the largest value of type T.

返回值: T –返回类型T的最大值。

Example:

例:

  1. Input:
    int a = 10;
    int b = 20;
    //finding largest value
    cout << max(a,b) << endl;
    Output: 
    20

In this example, we are going to find the largest values from given values of different types.

在此示例中,我们将从不同类型的给定值中找到最大值。

  1. #include <iostream>
    #include <algorithm> 
    using namespace std;
    int main()
    {
    cout << "max(10,20) : " << max(10, 20) << endl;//max(10,20) : 20
    cout << "max(10.23f,20.12f): " << max(10.23f, 20.12f) << endl; //max(10.23f,20.12f): 20.12
    cout << "max(-10,-20) : " << max(-10, -20) << endl;//max(-10,-20) : -10
    cout << "max('A','a') : " << max('A', 'a') << endl;//max('A','a') : a
    cout << "max('A','Z') : " << max('A', 'Z') << endl;//max('A','Z') : Z
    cout << "max(10,10) : " << max(10, 10) << endl;//max(10,10) : 10,输出第一个最大值
    return 0;
     
    }

2.min_element(), max_element()

min_element() 函数是 algorithm标头的库函数,用于从范围中寻找最小的元素,它接受一个容器范围[开始,结束],并返回一个指向给定范围内具有最小值的元素的迭代器。

此外,它可以接受一个函数作为第三个参数,该函数将对所有元素执行条件检查。

注意:使用 min_element() 函数 - 包括<algorithm>标题或者您可以简单使用<bits/stdc++.h>头文件。

std::min_element() 函数的语法

std::min_element(iterator start, iterator end, [compare comp]);

参数:

  • iterator start, iterator end- 这些是指向容器中范围的迭代器位置。
  • [compare comp]- 它是一个可选参数(一个函数),用于与给定范围内的元素进行比较。

返回值: iterator- 它返回一个迭代器,指向给定范围内具有最小值的元素。

例:

 Input:
    int arr[] = { 100, 200, -100, 300, 400 };
    
    //finding smallest element
    int result = *min_element(arr + 0, arr + 5);
    cout << result << endl;
    
    Output:
    -100

在这个程序中,我们有一个数组和一个向量并找到它们的最小元素。

//C++ STL program to demonstrate use of 
//std::min_element() function
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int main()
{
    //an array
    int arr[] = { 100, 200, -100, 300, 400 };

    //a vector
    vector<int> v1{ 10, 20, 30, 40, 50 };

    //finding smallest element from the array
    int result = *min_element(arr + 0, arr + 5);
    cout << "smallest element of the array:" << result << endl;

    //finding smallest element from the vector
    result = *min_element(v1.begin(), v1.end());
    cout << "smallest element of the vector:" << result << endl;

    return 0;
}
输出

smallest element of the array:-100
smallest element of the vector:10
min_element(first,end,cmp); 

1) 第三个参数cmp可写可不写, max_element() 和 min_element() 默认是从小到大排列,max_element() 输出最后一个值, min_element() 输出第一个值,但是如果自定义了cmp函数,则按照 cmp函数来。

2) 可以用于 vector 也可以用于 int arr[4] 或者string arr[4] ,也可以用于结构体vector或者结构体数组。

#include<iostream>
#include<algorithm>
using namespace std;
bool cmp(int a,int b){
return a > b;
}
int main(){
int num[]={2,3,1,6,4,5};
cout << "最小值是 " << *min_element(num,num+6) << endl;
cout << "最大值是 " << *max_element(num,num+6) << endl;
cout << "最小值是 " << *min_element(num,num+6,cmp) << endl;
cout << "最大值是 " << *max_element(num,num+6,cmp) << endl;
return 0;
}

3. __gcd(x,y) 最大公约数

#include<cstdio>
#include<algorithm>
using namespace std;
int n,m;
int main()
{
scanf("%d %d",&n,&m);
int k=__gcd(n,m);//最大公约数
printf("%d",k);
return 0;
}

4.std::min_element 与 std::min 的区别(max_element与max同)

std::min一般用于求 a 与 b 的较小者 或者求 initializer_list ilist 中值的最小者。
std::min_element是求一个范围内的最小者的迭代器。范围可以是全部容器,也可以是容器的一个子区间。
所以它们的适用范围和返回值不一样。

 

Reference: C++ std::max()C++ std::min_element()用法及代码示例 - 纯净天空 (vimsky.com)

 

posted @ 2022-01-22 11:52  Grit_L。  阅读(2766)  评论(0编辑  收藏  举报