ในบทช่วยสอนนี้ เราจะพูดถึงโปรแกรมเพื่อทำความเข้าใจวิธีย้อนกลับเวกเตอร์โดยใช้ STL ใน C++
สำหรับการย้อนกลับเวกเตอร์ที่กำหนด เราจะใช้ฟังก์ชัน reverse() จากไลบรารี STL ใน C++
ตัวอย่าง
#include <bits/stdc++.h>
using namespace std;
int main(){
//collecting 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;
//reversing the vector
reverse(a.begin(), a.end());
cout << "Reversed Vector: ";
for (int i = 0; i < a.size(); i++)
cout << a[i] << " ";
cout << endl;
return 0;
} ผลลัพธ์
Vector: 1 45 54 71 76 12 Reversed Vector: 12 76 71 54 45 1