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

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函数

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值