stl向量最大值
Given a vector and we have to find the smallest (minimum) and largest (maximum) elements.
給定一個向量,我們必須找到最小(最小)和最大(最大)元素。
查找向量的最小和最大元素 (Finding vector's minimum & maximum elements)
To find minimum element of a vector, we use *min_element() function and to find the maximum element, we use *max_element() function.
要找到向量的最小元素,請使用* min_element()函數 ,要找到最大元素,請使用* max_element()函數 。
Syntax:
句法:
*max_element(iterator start, iterator end);
*min_element(iterator start, iterator end);
Here, iterator start, iterator end are the iterator positions in the vector between them we have to find the minimum and maximum value.
在這里, 迭代器開始,迭代器結束是它們之間向量中迭代器的位置,我們必須找到最小值和最大值。
These functions are defined in <algorithm> header or we can use <bits/stdc++.h> header file.
這些函數在<algorithm>頭文件中定義,或者我們可以使用<bits / stdc ++。h>頭文件。
Example:
例:
Input:
vector<int> v1{ 10, 20, 30, 40, 50, 25, 15 };
cout << *min_element(v1.begin(), v1.end()) << endl;
cout << *max_element(v1.begin(), v1.end()) << endl;
Output:
10
50
C ++ STL程序查找向量的最小和最大元素 (C++ STL program to find minimum and maximum elements of a vector )
//C++ STL program to append a vector to a vector
#include <bits/stdc++.h>
using namespace std;
int main()
{
//vector declaration
vector<int> v1{ 10, 20, 30, 40, 50, 25, 15 };
int min = 0;
int max = 0;
//finding minimum & maximum element
min = *min_element(v1.begin(), v1.end());
max = *max_element(v1.begin(), v1.end());
cout << "minimum element of the vector: " << min << endl;
cout << "maximum element of the vector: " << max << endl;
return 0;
}
Output
輸出量
minimum element of the vector: 10
maximum element of the vector: 50
翻譯自: https://www.includehelp.com/stl/minimum-and-maximum-elements-of-a-vector.aspx
stl向量最大值