日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

stl max函数_std :: max_element()函数以及C ++ STL中的示例

發(fā)布時間:2025/3/11 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 stl max函数_std :: max_element()函数以及C ++ STL中的示例 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

stl max函數(shù)

C ++ STL std :: max_element()函數(shù) (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()函數(shù)是算法標(biāo)頭的庫函數(shù),用于查找范圍中的最大元素,它接受容器范圍[start,end],并返回一個迭代器,該迭代器指向給定范圍中具有最大值的元素。

Additionally, it can accept a function as the third argument that will perform a conditional check on all elements.

此外,它可以接受函數(shù)作為第三個參數(shù),它將對所有元素執(zhí)行條件檢查。

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

注意:要使用max_element()函數(shù) –包括<algorithm>頭文件,或者您可以簡單地使用<bits / stdc ++。h>頭文件。

Syntax of std::max_element() function

std :: max_element()函數(shù)的語法

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

Parameter(s):

參數(shù):

  • iterator start, iterator end – these are the iterator positions pointing to the ranges in the container.

    迭代器開始,迭代器結(jié)束 –這些是指向容器中范圍的迭代器位置。

  • [compare comp] – it's an optional parameter (a function) to be compared with elements in the given range.

    [compare comp] –它是一個可選參數(shù)(一個函數(shù)),可以與給定范圍內(nèi)的元素進(jìn)行比較。

Return value: iterator – it returns an iterator pointing to the element with the largest value in the given range.

返回值: iterator –它返回一個迭代器,該迭代器指向給定范圍內(nèi)具有最大值的元素。

Example:

例:

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

C ++ STL程序演示了std :: max_element()函數(shù)的使用 (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.

在此程序中,我們有一個數(shù)組和一個向量,并找到它們的最大元素。

//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: 50

Reference: C++ std::max_element()

參考: C ++ std :: max_element()

翻譯自: https://www.includehelp.com/stl/std-max_element-function-with-example.aspx

stl max函數(shù)

總結(jié)

以上是生活随笔為你收集整理的stl max函数_std :: max_element()函数以及C ++ STL中的示例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。