stl max函数

C ++ STL std :: max_element()函数 (C++ STL std::max_element() function)

max_element() function is a library function of algorithm header, it is used to find the largest element from the range, it accepts a container range [start, end] and returns an iterator pointing to the element with the largest value in the given range.

max_element()函数算法标头的库函数,用于查找范围中的最大元素,它接受容器范围[start,end],并返回一个迭代器,该迭代器指向给定范围中具有最大值的元素。

Additionally, it can accept a function as the third argument that will perform a conditional check on all elements.

此外,它可以接受函数作为第三个参数,它将对所有元素执行条件检查。

Note: To use max_element() function – include <algorithm> header or you can simple use <bits/stdc++.h> header file.

注意:要使用max_element()函数 –包括<algorithm>头文件,或者您可以简单地使用<bits / stdc ++。h>头文件。

Syntax of std::max_element() function

std :: max_element()函数的语法

    std::max_element(iterator start, iterator end, [compare comp]);

Parameter(s):

参数:

  • iterator start, iterator end – these are the iterator positions pointing to the ranges in the container.

    迭代器开始,迭代器结束 –这些是指向容器中范围的迭代器位置。

  • [compare comp] – it's an optional parameter (a function) to be compared with elements in the given range.

    [compare comp] –它是一个可选参数(一个函数),可以与给定范围内的元素进行比较。

Return value: iterator – it returns an iterator pointing to the element with the largest value in the given range.

返回值: iterator –它返回一个迭代器,该迭代器指向给定范围内具有最大值的元素。

Example:

例:

    Input:
int arr[] = { 100, 200, -100, 300, 400 };
//finding largest element
int result = *max_element(arr + 0, arr + 5);
cout << result << endl;
Output:
400

C ++ STL程序演示了std :: max_element()函数的使用 (C++ STL program to demonstrate use of std::max_element() function)

In this program, we have an array and a vector and finding their largest elements.

在此程序中,我们有一个数组和一个向量,并找到它们的最大元素。

//C++ STL program to demonstrate use of
//std::max_element() function
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{//an array
int arr[] = { 100, 200, -100, 300, 400 };
//a vector
vector<int> v1{ 10, 20, 30, 40, 50 };
//finding largest element from the array
int result = *max_element(arr + 0, arr + 5);
cout << "largest element of the array: " << result << endl;
//finding largest element from the vector
result = *max_element(v1.begin(), v1.end());
cout << "largest element of the vector: " << result << endl;
return 0;
}

Output

输出量

largest element of the array: 400
largest element of the vector: 50

Reference: C++ std::max_element()

参考: C ++ std :: max_element()

翻译自: https://www.includehelp.com/stl/std-max_element-function-with-example.aspx

stl max函数

stl max函数_std :: max_element()函数以及C ++ STL中的示例相关推荐

  1. matlab中now函数_now()方法以及JavaScript中的示例

    matlab中now函数 JavaScript now()方法 (JavaScript now() method) now() method is a Date class method, it is ...

  2. stl max函数_C ++ STL中带有示例的array :: max_size()函数

    stl max函数 C ++ STL array :: max_size()函数 (C++ STL array::max_size() function) max_size() function is ...

  3. stl中copy()函数_std :: copy_if()函数以及C ++ STL中的示例

    stl中copy()函数 C ++ STL std :: copy_if()函数 (C++ STL std::copy_if() function) copy_if() function is a l ...

  4. stl min函数_std :: min_element()函数以及C ++ STL中的示例

    stl min函数 C ++ STL std :: min_element()函数 (C++ STL std::min_element() function) min_element() functi ...

  5. c++minmax函数_std :: minmax()函数以及C ++ STL中的示例

    c++minmax函数 C ++ STL std :: minmax()函数 (C++ STL std::minmax() function) minmax() function is a libra ...

  6. C++--问题35--min和max函数和minmax函数的用法

    C++--问题35--min和max函数和minmax函数的用法 1.max函数 (1)C = max(A, B);返回队列中的最大值. #define max(a,b)            ((( ...

  7. pandas使用max函数和min函数计算dataframe日期(时间)数据列中最大日期和最小日期对应的数据行(maximum and minimum date or time row)

    pandas使用max函数和min函数计算dataframe日期(时间)数据列中最大日期和最小日期对应的数据行(maximum and minimum date or time row in data ...

  8. C++__min_element()和max_element()函数的使用

    C++__min_element()和max_element()函数的使用 今天在leetcode练习题目时遇到了min_element(),在此学习下min_element()与max_elemen ...

  9. STL库容器vector at函数

    STL库容器vector at函数 v.at(n)返回v指定位置n的元素.at函数比[]运算符更加安全, 因为它不会让你去访问到越界的元素,一旦越界将会在运行时中止程序并抛出一个异常std::out_ ...

最新文章

  1. 【linux】Valgrind工具集详解(四):抑制错误
  2. C# Excel转换为Json
  3. Redis批量操作详解及性能分析
  4. leetcode 227. Basic Calculator II | 227. 基本计算器 II(中缀表达式求值)
  5. SAP Spartacus 手动开启服务器端渲染 (SSR) 所必须的步骤
  6. LeetCode 1304. 和为零的N个唯一整数
  7. Java微服务篇2——SpringCloud
  8. Java:选择排序法
  9. 怎么选一个合适的手机?需要大内存,价格不太高的?
  10. JDK API 中英文版 在线版、下载地址
  11. Java调用cplex求解泊位分配模型_CPLEX约束问题
  12. DOSBox 0.74 汇编 out of memery test.asm(2):out of memory
  13. 服务器微信互通是什么意思,妄想山海微信区和QQ区互通吗,服务器互通数据详解...
  14. IOS7使用吐槽(抛弃拟物化您还能走多远.........)
  15. html5点赞计数功能,点赞功能
  16. Component series.line not exists. Load it first.
  17. 工作经费的开支范围_经费支出范围
  18. Facebook SDK for iOS 2.4 iOS 6 上运行崩溃
  19. oracle如何导出表结构及数据,PLSQL怎样导出oracle表结构和数据
  20. C#读Visio模型数据

热门文章

  1. mongotemplate中save抛出异常_异常处理的三个好习惯 | Python 工匠
  2. python提取数据库数据_Python如何读取MySQL数据库表数据
  3. java 建造者实际中的用法_java中j建造者模式详解和使用方法
  4. Iptables防火墙原理
  5. MIP 技术交流分享(3月15日)
  6. 前端页面:一直报Cannot set property 'height' of undefined
  7. tomcat7的数据库连接池tomcatjdbc的25个优势
  8. [转]“Ceph浅析”系列之(—)—Ceph概况
  9. Go语言Web框架gwk介绍 (一)
  10. 华为防火墙Edumon1000E配置