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

时间:2025-03-21 08:06:54

c++ stl 获取最小值

Given a vector and we have to minimum/smallest element using C++ STL program.

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

寻找向量的最小元素 (Finding smallest element of a vector)

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

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

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

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

Syntax:

句法:

    *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 value.

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

Example:

例:

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

    cout << *min_element((), ()) << endl;

    Output:
    10

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

//C++ STL program to find minimum or smallest 
//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 minimum element
    int min = *min_element((), ());

    cout << "minimum/smallest element is: " << min << endl;

    return 0;
}

Output

输出量

vector elements...
10 20 30 40 50
minimum/smallest element is: 10


翻译自: /stl/

c++ stl 获取最小值