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
热门推荐
10 爱情贺卡祝福语简短英语
11 娃娃考试祝福语幽默简短
12 结婚祝福语简短精辟 兄弟
13 表达朋友的祝福语简短
14 春节感恩祝福语简短的
15 结婚祝福语粤语大全简短
16 恭喜朋友升职祝福语简短
17 圣诞饭店祝福语大全简短
18 建工系毕业祝福语简短