ในบทช่วยสอนนี้ เราจะพูดถึงโปรแกรมเพื่อทำความเข้าใจวิธีค้นหาองค์ประกอบสูงสุดของเวกเตอร์โดยใช้ STL ใน C++
ในการค้นหาองค์ประกอบสูงสุดจากเวกเตอร์ที่กำหนด เราจะใช้เมธอด *max_element() จากไลบรารี STL
ตัวอย่าง
#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