我是靠谱客的博主 独特蜻蜓,这篇文章主要介绍C++中vector的使用1. c++ Vector中的获取最大元素2. C++求vector容器中的最大值(最小值)及其位置,现在分享给大家,希望可以做个参考。
C++中vector的使用
- 1. c++ Vector中的获取最大元素
- 2. C++求vector容器中的最大值(最小值)及其位置
1. c++ Vector中的获取最大元素
复制代码
1
2
3
4
5
6
7#include<vector> vector<int>v1; ...... int v1_max = *max_element(v1.begin(),v1.end());
使用vector中的max_element
(a,b)函数,可返回向量[a,b]区间内的最大元素的地址。做 * 后可得到相应的元素值。
当需要得到某一区间内的最大值时[a,b)
,参数为地址类型。如:v1_max = * max_element(&v1[a],&v1[b])
;(此处区间为前闭后开
!)
参考:c++ Vector中的获取最大元素
2. C++求vector容器中的最大值(最小值)及其位置
方法:
min_element
和max_element
输入参数为vector
迭代器,输出为单一元素迭代器
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16#include <iostream> #include <vector> #include <algorithm> using namespace std; int main(){ vector<int> a = { 2,4,6,7,1,0,8,9,6,3,2 }; auto maxPosition = max_element(a.begin(), a.end()); auto minPosition = min_element(a.begin(), a.end()); cout << *maxPosition << " at the postion of " << maxPosition - a.begin() <<endl; cout << *minPosition << " at the postion of " << maxPosition - a.begin() <<endl; system("pause"); return 0; }
参考:C++求vector容器中的最大值(最小值)及其位置
最后
以上就是独特蜻蜓最近收集整理的关于C++中vector的使用1. c++ Vector中的获取最大元素2. C++求vector容器中的最大值(最小值)及其位置的全部内容,更多相关C++中vector的使用1.内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复