stl向量最大值
Given a vector and we have to maximum/largest element using C++ STL program.
给定一个向量,我们必须使用C ++ STL程序使用最大/最大元素。
寻找向量的最大元素 (Finding largest element of a vector)
To find a largest or maximum element of a vector, we can use *max_element() function which is defined in <algorithm> header. It accepts a range of iterators from which we have to find the maximum / largest element and returns the iterator pointing the maximum element between the given range.
要查找向量的最大或最大元素 ,我们可以使用在<algorithm>标头中定义的* max_element()函数 。 它接受一定范围的迭代器,我们必须从中查找最大/最大元素,并返回指向给定范围之间的最大元素的迭代器。
Note: To use vector – include <vector> header, and to use *max_element() function – include <algorithm> header or we can simply use <bits/stdc++.h> header file.
注意:要使用vector –包含<vector>头文件,并使用* max_element()函数 –包含<algorithm>头文件,或者我们可以简单地使用<bits / stdc ++。h>头文件。
Syntax:
句法:
*max_element(iterator start, iterator end);
Here, iterator start, iterator end are the iterator positions in the vector between them we have to find the maximum value.
在这里, 迭代器开始,迭代器结束是它们之间向量中的迭代器位置,我们必须找到最大值。
Example:
例:
Input:
vector<int> v1{ 10, 20, 30, 40, 50, 25, 15 };
cout << *max_element(v1.begin(), v1.end()) << endl;
Output:
50
C ++ STL程序查找向量的最大或最大元素 (C++ STL program to find maximum or largest element of a vector )
//C++ STL program to find maximum or largest
//element of a vector
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
//vector
vector<int> v1{ 10, 20, 30, 40, 50 };
//printing elements
cout << "vector elements..." << endl;
for (int x : v1)
cout << x << " ";
cout << endl;
//finding the maximum element
int max = *max_element(v1.begin(), v1.end());
cout << "maximum/largest element is: " << max << endl;
return 0;
}
Output
输出量
vector elements...
10 20 30 40 50
maximum/largest element is: 50
翻译自: https://www.includehelp.com/stl/find-the-maximum-largest-element-of-a-vector.aspx
stl向量最大值