c++ stl 获取最小值_如何在C ++ STL中找到向量的最小/最小元素?
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>標(biāo)頭中定義的* min_element()函數(shù) 。 它接受一定范圍的迭代器,我們必須從中查找最小/最小元素,然后返回指向給定范圍之間的最小元素的迭代器。
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()函數(shù) –包含<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.
在這里, 迭代器開始,迭代器結(jié)束是它們之間向量中的迭代器位置,我們必須找到最小值。
Example:
例:
Input:vector<int> v1{ 10, 20, 30, 40, 50, 25, 15 };cout << *min_element(v1.begin(), v1.end()) << endl;Output:10C ++ 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() {//vectorvector<int> v1{ 10, 20, 30, 40, 50 };//printing elementscout << "vector elements..." << endl;for (int x : v1)cout << x << " ";cout << endl;//finding the minimum elementint min = *min_element(v1.begin(), v1.end());cout << "minimum/smallest element is: " << min << endl;return 0; }Output
輸出量
vector elements... 10 20 30 40 50 minimum/smallest element is: 10翻譯自: https://www.includehelp.com/stl/find-the-minimum-smallest-element-of-a-vector.aspx
c++ stl 獲取最小值
總結(jié)
以上是生活随笔為你收集整理的c++ stl 获取最小值_如何在C ++ STL中找到向量的最小/最小元素?的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java notify唤醒原理_Java
- 下一篇: 十六进制转八进制c++代码_如何将十六进