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)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM