stl向量最大值_C ++ STL中向量的最小和最大元素

stl向量最大值

Given a vector and we have to find the smallest (minimum) and largest (maximum) elements.

给定一个向量,我们必须找到最小(最小)和最大(最大)元素。

查找向量的最小和最大元素 (Finding vector's minimum & maximum elements)

To find minimum element of a vector, we use *min_element() function and to find the maximum element, we use *max_element() function.

要找到向量的最小元素,请使用* min_element()函数 ,要找到最大元素,请使用* max_element()函数

Syntax:

句法:

    *max_element(iterator start, iterator end);
    *min_element(iterator start, iterator end);

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

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

These functions are defined in <algorithm> header or we can use <bits/stdc++.h> header file.

这些函数在<algorithm>头文件中定义,或者我们可以使用<bits / stdc ++。h>头文件。

Example:

例:

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

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

    Output:
    10
    50

C ++ STL程序查找向量的最小和最大元素 (C++ STL program to find minimum and maximum elements of a vector )

//C++ STL program to append a vector to a vector
#include <bits/stdc++.h>
using namespace std;

int main()
{
    //vector declaration
    vector<int> v1{ 10, 20, 30, 40, 50, 25, 15 };
    int min = 0;
    int max = 0;

    //finding minimum  & maximum element
    min = *min_element(v1.begin(), v1.end());
    max = *max_element(v1.begin(), v1.end());

    cout << "minimum element of the vector: " << min << endl;
    cout << "maximum element of the vector: " << max << endl;

    return 0;
}

Output

输出量

minimum element of the vector: 10
maximum element of the vector: 50


翻译自: https://www.includehelp.com/stl/minimum-and-maximum-elements-of-a-vector.aspx

stl向量最大值

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值