C ++ STL中向量的最小和最大元素
给定一个向量,我们必须找到最小(最小)和最大(最大)元素。
查找向量的最小和最大元素
要找到向量的最小元素,请使用*min_element()函数,要找到最大元素,请使用*max_element()函数。
语法:
*max_element(iterator start, iterator end);
*min_element(iterator start, iterator end);在这里,迭代器开始,迭代器结束是它们之间向量中迭代器的位置,我们必须找到最小值和最大值。
这些函数在<algorithm>头文件中定义,或者我们可以使用<bits/stdc++。h>头文件。
示例
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
50C++STL程序查找向量的最小和最大元素
//C++STL程序将向量附加到向量
#include <bits/stdc++.h>
using namespace std;
int main(){
//向量声明
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;
}输出结果
minimum element of the vector: 10 maximum element of the vector: 50
热门推荐
3 祝福语简短的微信
10 幼儿祝福语大全简短贺卡
11 感谢教官文案祝福语简短
12 鼠年同事祝福语简短创意
13 元旦祝福语 简短独特群发
14 妈咪跟我生日祝福语简短
15 给儿子新婚祝福语简短
16 伴娘祝福语长文案简短
17 恭喜高考完祝福语简短
18 生日祝福语送老公简短