ที่นี่เราจะดูฟังก์ชัน crbegin() และ crend() ของ array ใน C++ STL
ฟังก์ชัน array::crbegin() ใช้ดึง reverse iterator คืนค่า constant reverse iterator ที่ชี้ไปยังองค์ประกอบสุดท้ายของคอนเทนเนอร์ ฟังก์ชันนี้ไม่รับพารามิเตอร์
ฟังก์ชัน array::crend() เป็นตรงข้ามของ crbegin() คืนค่า iterator ที่ชี้ไปยังองค์ประกอบสุดท้ายของ reverse iterator
มาดูโค้ดตัวอย่างเพื่อเข้าใจดีขึ้น
ตัวอย่าง
#include<iostream>
#include<array>
using namespace std;
int main() {
array<int, 10> arr = {00, 11, 22, 33, 44, 55, 66, 77, 88, 99};
cout << "The list in reverse order: ";
for(auto it = arr.crbegin(); it != arr.crend(); it++) {
cout << *it << " ";
}
}
ผลลัพธ์
The list in reverse order: 99 88 77 66 55 44 33 22 11 0