Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C++

วิธีหาองค์ประกอบสูงสุดของ Vector ด้วย STL ใน C++


ในบทช่วยสอนนี้ เราจะอภิบายโปรแกรมเพื่อทำความเข้าใจวิธีหาองค์ประกอบสูงสุดของ vector โดยใช้ STL ใน C++

เพื่อหาองค์ประกอบสูงสุดจาก vector ที่กำหนด เราจะใช้เมธอด *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