stl向量最大值_如何在C ++ STL中找到向量的最大/最大元素?

stl向量最大值

Given a vector and we have to maximum/largest element using C++ STL program.

给定一个向量,我们必须使用C ++ STL程序使用最大/最大元素。

寻找向量的最大元素 (Finding largest element of a vector)

To find a largest or maximum element of a vector, we can use *max_element() function which is defined in <algorithm> header. It accepts a range of iterators from which we have to find the maximum / largest element and returns the iterator pointing the maximum element between the given range.

查找向量的最大或最大元素 ,我们可以使用在<algorithm>标头中定义的* max_element()函数 。 它接受一定范围的迭代器,我们必须从中查找最大/最大元素,并返回指向给定范围之间的最大元素的迭代器。

Note: To use vector – include <vector> header, and to use *max_element() function – include <algorithm> header or we can simply use <bits/stdc++.h> header file.

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

Syntax:

句法:

    *max_element(iterator start, iterator end);

Here, iterator start, iterator end are the iterator positions in the vector between them we have to find the maximum value.

在这里, 迭代器开始,迭代器结束是它们之间向量中的迭代器位置,我们必须找到最大值。

Example:

例:

    Input:
    vector<int> v1{ 10, 20, 30, 40, 50, 25, 15 };

    cout << *max_element(v1.begin(), v1.end()) << endl;

    Output:
    50

C ++ STL程序查找向量的最大或最大元素 (C++ STL program to find maximum or largest element of a vector )

//C++ STL program to find maximum or largest 
//element of a vector 
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int main()
{
    //vector
    vector<int> v1{ 10, 20, 30, 40, 50 };

    //printing elements
    cout << "vector elements..." << endl;
    for (int x : v1)
        cout << x << " ";
    cout << endl;

    //finding the maximum element
    int max = *max_element(v1.begin(), v1.end());

    cout << "maximum/largest element is: " << max << endl;

    return 0;
}

Output

输出量

vector elements...
10 20 30 40 50
maximum/largest element is: 50


翻译自: https://www.includehelp.com/stl/find-the-maximum-largest-element-of-a-vector.aspx

stl向量最大值

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值