可以用max_element()及min_element()函数,二者返回的都是迭代器或指针。

头文件:#include<algorithm>

1.求数组的最大值或最小值

1)vector容器

例 vector<int> vec

最大值:int maxValue = *max_element(v.begin(),v.end());

最小值:int minValue = *min_element(v.begin(),v.end());

2)普通数组

例 a[]={1,2,3,4,5,6};

最大值:int maxValue = *max_element(a,a+6);

最小值:int minValue = *min_element(a,a+6);

2.求数组最大值最小值对应的下标

1)vector容器

例 vector<int> vec

最大值下标:int maxPosition = max_element(v.begin(),v.end()) - v.begin();

最小值下标:int minPosition = min_element(v.begin(),v.end()) - v.begin();

2)普通数组

例 a[]={1,2,3,4,5,6};

最大值下标:int maxPosition = max_element(a,a+6) - a;

最小值下标:int minPosition = min_element(a,a+6) - a;

注意:返回的是第一个最大(小)元素的位置。

参考:

C++笔记——std::min_element和std::max_element

C++中Vector求最大值最小值

C++ 数组或vector最大值最小值

C++数组或vector求最大值最小值的更多相关文章

  1. C++中Vector求最大值最小值

    vector<int> v: 最大值: int max = *max_element(v.begin(),v.end()); 最小值: int min = *min_element(v.b ...

  2. 【C++】Vector求最大值最小值

    最大值: int max = *max_element(v.begin(),v.end()); 最小值: int min = *min_element(v.begin(),v.end());

  3. html标签内部简单加js 一维数组求最大值 最小值两个值位置和数字金字塔图形

     html标签内部,简单加js <a href=""></a><!DOCTYPE html PUBLIC "-//W3C//DTD XHTM ...

  4. js求最大值最小值

    比较数组中数值的大小是比较常见的操作,比较大小的方法有多种,比如可以使用自带的sort()函数,代码如下: <html> <head> <meta charset=&qu ...

  5. java数组回顾---线性查找最大值最小值---二分查找

    import java.util.Scanner; public class ArrayDemo { public static void main(String []args) { //------ ...

  6. Day_11【集合】扩展案例5_对list集合对象中的元素进行反转,求最大值最小值,求元素i在list集合中首次出现的索引,将oldvalue替换为newvalue

    分析以下需求,并用代码实现 定义MyArrays工具类,该工具类中有以下方法,方法描述如下: 1.public static void reverse(ArrayList<Integer> ...

  7. 求最大值最小值的方法 时间复杂度O(n)

    #include<iostream> #include <iostream> #include <bitset> #include <ctime> us ...

  8. MapReduce求最大值最小值问题

    import java.io.File; import java.io.IOException; import org.apache.hadoop.conf.Configuration; import ...

  9. 无序数组a,求a[i]-a[j]的最大值,且i<j

    一道面试题:对于无序数组a,求a[i]-a[j]的最大值,其中i<j package test; import java.util.Arrays; public class FindMax { ...

随机推荐

  1. jquery网页定位导航特效

    <!DOCTYPE html> <html lang="en"> <head> <script src="http://code ...

  2. Python从入门到精通视频(全60集)✍✍✍

    Python从入门到精通视频(全60集)  整个课程都看完了,这个课程的分享可以往下看,下面有链接,之前做java开发也做了一些年头,也分享下自己看这个视频的感受,单论单个知识点课程本身没问题,大家看 ...

  3. 【转】Java程序CPU飙升问题排查方法

    windows环境下cpu飙升问题 线上某台runtime机器(windows Server)cpu报警,这种情况初步就是代码里面死循环了,先把机器下线了保证不再有新的任务分配进来,然而cpu使用依然 ...

  4. error while loading shared libraries: lib*.so: cannot open shared object file: No such file or directory

    动态库的搜索路径搜索的先后顺序是: 1.编译目标代码时指定的动态库搜索路径; 2.环境变量LD_LIBRARY_PATH指定的动态库搜索路径: 比如export LD_LIBRARY_PATH=/us ...

  5. canvas插入图片设置背景,渐变

    ##在canvas中插入图片(需要image对象) 1.canvas操作图片时,必须要等图片加载完才能操作 2.drawImage(image, x, y, width, height) 其中 ima ...

  6. Java 多线程 - synchronized与Lock的区别

    https://blog.csdn.net/qq_39521554/article/details/81130442 http://www.cnblogs.com/huangbw/p/8516024. ...

  7. C++ 贪吃蛇二维

    #include <iostream> #include <conio.h> #include <windows.h> #include <time.h> ...

  8. 学无止境,我爱python

    每天更新.... python基础 python字符编码 python基础数据类型 python列表.元组 python字典 python字符编码unicode,utf-8,ascii python深 ...

  9. 树形dp经典换根法——cf1187E

    假设以u为根时结果是tot,现在转换到了以u的儿子v为根,那么结果变成了tot-size[v]+(sizetot-size[v]) 根据这个转移方程,先求出以1为根的tot,然后dfs一次转移即可 # ...

  10. 实用的 atom 插件

    推荐几款我喜欢的Atom插件 时间 2017-05-05 09:00:00  Hi Linux 原文  http://www.hi-linux.com/posts/28459.html 主题 Atom ...