如何在 C++ 中使用 STL 找出向量的最大元素?
在本教程中,我們將討論一個程式,以瞭解如何在 C++ 中使用 STL 查詢向量的最大元素。
要查詢給定向量的最大元素,我們將使用 STL 庫中的 *max_element() 方法。
示例
#include <bits/stdc++.h>
using namespace std;
int main(){
//defining the vector
vector<int> a = { 1, 45, 54, 71, 76, 12 };
cout << "Vector: ";
for (int i = 0; i < a.size(); i++)
cout << a[i] << " ";
cout << endl;
//finding the maximum element
cout << "Max Element = " << *max_element(a.begin(), a.end());
return 0;
}輸出
Vector: 1 45 54 71 76 12 Max Element = 76
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP