stl向量最大值_查找向量中的最大和最小元素| C ++ STL

本文探讨了在STL向量中寻找最大和最小元素的两种方法:一是通过遍历向量并更新最大和最小值;二是利用标准库函数std::max_element和std::min_element,这些函数在<algorithm>头文件中定义,时间复杂度为线性。
摘要由CSDN通过智能技术生成

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向量最大值

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值