stl向量最大值
A lot of problems in programming challenges require finding a largest and smallest among the given elements. in this article we discuss two methods one is to initialize max as first element, then traverse the vector from index 1 to size-1 and for every traversed element, compare it with max, if it is greater than max, then update max is equal to element. For finding smallest element we do same steps i.e initialize min as first element and for every traversed element, compare it with min, if it is smaller than min, then update min.
编程挑战中的许多问题都需要在给定元素中找到最大和最小的地方 。 在本文中,我们讨论两种方法:一种是将max初始化为第一个元素,然后将向量从索引1遍历到size-1,然后将每个遍历的元素与max进行比较,如果它大于max ,则更新max等于到元素。 为了找到最小的元素,我们执行相同的步骤,即将min初始化为第一个元素,并且对于每个遍历的元素,将其与min进行比较(如果小于min ,则更新min) 。
Another method is finding largest and smallest by using library functions std::max_element and std::min_element, respectively. These methods are defined in <algorithm> header. Time complexity of both the methods is linear i.e the ta(n).
另一种方法是分别使用库函数std :: max_element和std :: min_element 查找最大和最小 。 这些方法在<algorithm>标头中定义。 两种方法的时间复杂度都是线性的,即ta(n) 。
Example 1: This example shows the working of first method we discussed above...
示例1:此示例显示了我们上面讨论的第一种方法的工作...
#include <bits/stdc++.h>
using namespace std;
//for finding maximum
int Findlarge(vector<int> myVector)
{
// Initialize maximum element
int max = myVector[0];
// Traverse vector elements
for (int i = 1; i < myVector.size(); i++)
if (myVector[i] > max)
max = myVector[i];
return max;
}
//for finding minimum
int FindMin(vector<int> myVector)
{
// Initialize minimum element
int min = myVector[0];
// Traverse vector elements
for (int i = 1; i < myVector.size(); i++)
if (myVector[i] < min)
min = myVector[i];
return min;
}
//Main function
int main()
{
vector<int> myVector;
myVector.push_back(1);
myVector.push_back(100);
myVector.push_back(76);
myVector.push_back(9);
cout<<endl<<endl<<endl<<endl;
cout<<"elements in Vector = ";
for(int i=0;i<myVector.size();i++)
{
cout<<myVector[i]<<" ";
}
cout<<endl;
cout << "Largest element given vector is = " << Findlarge(myVector)<<endl;
cout << "smallest element given vector is = " << FindMin(myVector)<<endl;
return 0;
}
Output
输出量
elements in Vector = 1 100 76 9
Largest element given vector is = 100
smallest element given vector is = 1
Example 2: Using std::max_element and std::min_element...
示例2:使用std :: max_element和std :: min_element ...
#include <bits/stdc++.h>
using namespace std;
int main()
{
vector<int> myVector;
myVector.push_back(1);
myVector.push_back(100);
myVector.push_back(76);
myVector.push_back(9);
cout<<"elements in Vector = ";
for(int i=0;i<myVector.size();i++)
{
cout<<myVector[i]<<" ";
}
cout<<endl;
cout << "Largest element given vector is = " ;
cout << *max_element(myVector.begin(), myVector.end())<<endl;
cout << "smallest element given vector is = ";
cout << *min_element(myVector.begin(), myVector.end());
return 0;
}
Output
输出量
elements in Vector = 1 100 76 9
Largest element given vector is = 100
smallest element given vector is = 1
翻译自: https://www.includehelp.com/stl/find-largest-and-smallest-elements-in-a-vector.aspx
stl向量最大值