stl max函数_std :: max_element()函数以及C ++ STL中的示例
stl max函數
C ++ STL std :: max_element()函數 (C++ STL std::max_element() function)
max_element() function is a library function of algorithm header, it is used to find the largest element from the range, it accepts a container range [start, end] and returns an iterator pointing to the element with the largest value in the given range.
max_element()函數是算法標頭的庫函數,用于查找范圍中的最大元素,它接受容器范圍[start,end],并返回一個迭代器,該迭代器指向給定范圍中具有最大值的元素。
Additionally, it can accept a function as the third argument that will perform a conditional check on all elements.
此外,它可以接受函數作為第三個參數,它將對所有元素執行條件檢查。
Note: To use max_element() function – include <algorithm> header or you can simple use <bits/stdc++.h> header file.
注意:要使用max_element()函數 –包括<algorithm>頭文件,或者您可以簡單地使用<bits / stdc ++。h>頭文件。
Syntax of std::max_element() function
std :: max_element()函數的語法
std::max_element(iterator start, iterator end, [compare comp]);Parameter(s):
參數:
iterator start, iterator end – these are the iterator positions pointing to the ranges in the container.
迭代器開始,迭代器結束 –這些是指向容器中范圍的迭代器位置。
[compare comp] – it's an optional parameter (a function) to be compared with elements in the given range.
[compare comp] –它是一個可選參數(一個函數),可以與給定范圍內的元素進行比較。
Return value: iterator – it returns an iterator pointing to the element with the largest value in the given range.
返回值: iterator –它返回一個迭代器,該迭代器指向給定范圍內具有最大值的元素。
Example:
例:
Input:int arr[] = { 100, 200, -100, 300, 400 };//finding largest elementint result = *max_element(arr + 0, arr + 5);cout << result << endl;Output:400C ++ STL程序演示了std :: max_element()函數的使用 (C++ STL program to demonstrate use of std::max_element() function)
In this program, we have an array and a vector and finding their largest elements.
在此程序中,我們有一個數組和一個向量,并找到它們的最大元素。
//C++ STL program to demonstrate use of //std::max_element() function #include <iostream> #include <algorithm> #include <vector> using namespace std;int main() {//an arrayint arr[] = { 100, 200, -100, 300, 400 };//a vectorvector<int> v1{ 10, 20, 30, 40, 50 };//finding largest element from the arrayint result = *max_element(arr + 0, arr + 5);cout << "largest element of the array: " << result << endl;//finding largest element from the vectorresult = *max_element(v1.begin(), v1.end());cout << "largest element of the vector: " << result << endl;return 0; }Output
輸出量
largest element of the array: 400 largest element of the vector: 50Reference: C++ std::max_element()
參考: C ++ std :: max_element()
翻譯自: https://www.includehelp.com/stl/std-max_element-function-with-example.aspx
stl max函數
總結
以上是生活随笔為你收集整理的stl max函数_std :: max_element()函数以及C ++ STL中的示例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python operator.trut
- 下一篇: Scala山脉