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

การทำงานกับ Array และ Vector ด้วย STL ใน C++


Array และ Vector เป็นโครงสร้างข้อมูลที่สำคัญมากในการเขียนโปรแกรมแข่งขัน (competitive programming) สำหรับการแก้ปัญหา และ STL (Standard Template Library) ใน C++ มีฟังก์ชันบางอย่างสำหรับดำเนินการกับ array และ vector

มาดูฟังก์ชันบางอย่างในการทำงาน:

การหาผลรวม ค่าต่ำสุด และค่าสูงสุดของ array/vector - ใน STL มีฟังก์ชันช่วยหาผลรวม ค่าสูงสุด และค่าต่ำสุดของ array/vector ฟังก์ชันพร้อมการทำงาน:

หาผลรวม

accumulate(startIndex, endIndex, initialSum)

องค์ประกอบที่มากที่สุดของ array/vector

*max_element(startIndex, endIndex)

องค์ประกอบที่น้อยที่สุดของ array/vector

*min_element(startIndex, endIndex)

โปรแกรมดำเนินการกับ Array

#include <bits/stdc++.h>
using namespace std;

int main() {
    int array[] = {65, 7, 12, 90, 31, 113};
    int l = sizeof(array) / sizeof(array[0]);
    cout << "The elements of the array are: ";
    for (int i = 0; i < l; i++)
        cout << array[i] << "\t";
    cout << endl;
    cout << "The sum of all elements of the array: " << accumulate(array, array + l, 0) << endl;
    cout << "The element with maximum value in array: " << *max_element(array, array + l) << endl;
    cout << "The element with minimum value in array: " << *min_element(array, array + l) << endl;
    return 0;
}

ผลลัพธ์

The elements of the array are: 65 7 12 90 31 113
The sum of all elements of the array: 318
The element with maximum value in array: 113
The element with minimum value in array: 7

โปรแกรมดำเนินการกับ Vector

#include <bits/stdc++.h>
using namespace std;

int main() {
    vector vec = {65, 7, 12, 90, 31, 113};
    cout << "The sum of all elements of the vector: " << accumulate(vec.begin(), vec.end(), 0) << endl;
    cout << "The element with maximum value in vector: " << *max_element(vec.begin(), vec.end()) << endl;
    cout << "The element with minimum value in vector: " << *min_element(vec.begin(), vec.end()) << endl;
    return 0;
}

ผลลัพธ์

The sum of all elements of the vector: 318
The element with maximum value in vector: 113
The element with minimum value in vector: 7

การจัดเรียง (Sorting) องค์ประกอบของ Array/Vector

ใน STL มีฟังก์ชัน sort() ซึ่งใช้จัดเรียงองค์ประกอบของ array/vector ฟังก์ชันนี้ใช้เทคนิค Quick Sort สำหรับจัดเรียง array/vector

Syntax

sort(startIndex, endIndex)

โปรแกรมจัดเรียงองค์ประกอบของ Array

#include <bits/stdc++.h>
using namespace std;

int main() {
    int array[] = {65, 7, 12, 90, 31, 113};
    int l = sizeof(array) / sizeof(array[0]);
    cout << "The elements of the array are: ";
    for (int i = 0; i < l; i++)
        cout << array[i] << "\t";
    cout << endl;
    cout << "\nSorting elements of the array...\n\n";
    sort(array, array + l);
    cout << "The sorted array is: ";
    for (int i = 0; i < l; i++)
        cout << array[i] << "\t";
    cout << endl;
    return 0;
}

ผลลัพธ์

The elements of the array are: 65 7 12 90 31 113
Sorting elements of the array...
The sorted array is: 7 12 31 65 90 113

โปรแกรมจัดเรียงองค์ประกอบของ Vector

#include <bits/stdc++.h>
using namespace std;

int main() {
    vector vec = {65, 7, 12, 90, 31, 113};
    cout << "The elements of the vector are: ";
    for (int i = 0; i < vec.size(); i++)
        cout << vec[i] << "\t";
    cout << endl;
    cout << "\nSorting elements of the vector...\n\n";
    sort(vec.begin(), vec.end());
    cout << "The sorted vector is: ";
    for (int i = 0; i < vec.size(); i++)
        cout << vec[i] << "\t";
    cout << endl;
    return 0;
}

ผลลัพธ์

The elements of the vector are: 65 7 12 90 31 113
Sorting elements of the vector...
The sorted vector is: 7 12 31 65 90 113