日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

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

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

stl min函數

C ++ STL std :: min_element()函數 (C++ STL std::min_element() function)

min_element() function is a library function of algorithm header, it is used to find the smallest element from the range, it accepts a container range [start, end] and returns an iterator pointing to the element with the smallest value in the given range.

min_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 min_element() function – include <algorithm> header or you can simple use <bits/stdc++.h> header file.

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

Syntax of std::min_element() function

std :: min_element()函數的語法

std::min_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 smallest value in the given range.

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

Example:

例:

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

C ++ STL程序演示std :: min_element()函數的使用 (C++ STL program to demonstrate use of std::min_element() function)

In this program, we have an array and a vector and finding their smallest elements.

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

//C++ STL program to demonstrate use of //std::min_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 smallest element from the arrayint result = *min_element(arr + 0, arr + 5);cout << "smallest element of the array: " << result << endl;//finding smallest element from the vectorresult = *min_element(v1.begin(), v1.end());cout << "smallest element of the vector: " << result << endl;return 0; }

Output

輸出量

smallest element of the array: -100 smallest element of the vector: 10

Reference: C++ std::min_element()

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

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

stl min函數

總結

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

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。