กำหนดให้เป็นหน้าที่แสดงการทำงานของฟังก์ชัน Deque crend( ) ใน C++ STL
เด็คคืออะไร
Deque คือ Double Ended Queues ซึ่งเป็นคอนเทนเนอร์ลำดับที่ให้การทำงานของการขยายและการหดตัวทั้งสองด้าน โครงสร้างข้อมูลคิวอนุญาตให้ผู้ใช้แทรกข้อมูลได้เฉพาะที่ END และลบข้อมูลออกจาก FRONT ลองมาเปรียบเทียบคิวที่ป้ายรถเมล์ที่คนสามารถแทรกลงในคิวจาก END เท่านั้นและบุคคลที่ยืนอยู่ด้านหน้าจะเป็นคนแรกที่ถูกลบในขณะที่ในคิวแบบ Double End การแทรกและการลบข้อมูลทำได้ทั้งสองแบบ จบ.
ฟังก์ชัน deque crend( ) จะคืนค่า const_reverse_iterator ที่ชี้ไปยังองค์ประกอบที่อยู่ข้างหน้าองค์ประกอบแรกของ deque ซึ่งถือเป็นการย้อนกลับ
ไวยากรณ์
Deque_name.crend( )
ผลตอบแทนที่ได้รับ
ฟังก์ชัน deque_crend( ) คืนค่า const_reverse_iterator ของ deque
ตัวอย่าง
ป้อนข้อมูล เด็ค − 5 4 3 2 1
ผลผลิต Deque ในลำดับย้อนกลับ − 1 2 3 4 5
ป้อนข้อมูล เด็ค − 75 45 33 77 12
ผลผลิต Deque ในลำดับย้อนกลับ − 12 77 33 45 75
แนวทางสามารถติดตามได้
-
ขั้นแรก เราประกาศเดค
-
จากนั้นเราก็พิมพ์ดีค
-
จากนั้นเราใช้ฟังก์ชัน crend( )
โดยใช้วิธีการข้างต้น เราสามารถพิมพ์ deque ในลำดับที่กลับกัน
ตัวอย่าง
// C++ code to demonstrate the working of deque crend( ) function
#include<iostream.h>
#include<deque.h>
Using namespace std;
int main ( ){
// declaring the deque
Deque<int> deque = { 5, 4, 3, 2, 1 };
// print the deque
cout<< “ Deque: “;
for( auto x = deque.begin( ); x != deque.end( ); ++x)
cout<< *x << “ “;
// printing deque in reverse order
cout<< “ Deque in reverse order:”;
for( auto x = deque.crend( ) - 1; x >= deque.begin( ); --x)
cout<< “ “ <<*x;
return 0;
} ผลลัพธ์
หากเรารันโค้ดด้านบน มันจะสร้างผลลัพธ์ต่อไปนี้
Input - Deque: 5 4 3 2 1 Output - Deque in reverse order: 1 2 3 4 5
ตัวอย่าง
// C++ code to demonstrate the working of crend( ) function
#include<iostream.h>
#include<deque.h>
Using namespace std;
int main( ){
deque<char> deque ={ ‘L’ , ‘A’ , ‘P’ , ‘T’ , ‘O’ , ‘P’ };
cout<< “ Deque: “;
for( auto x = deque.begin( ); x != deque.end( ); ++x)
cout<< *x << “ “;
// printing deque in reverse order
cout<< “ Deque in reverse order:”;
for( auto x = deque.crend( ) - 1; x >= deque.begin( ); --x)
cout<< “ “ <<*x;
return 0;
} ผลลัพธ์
หากเรารันโค้ดด้านบน มันจะสร้างผลลัพธ์ต่อไปนี้
Input – Deque: L A P T O P Output – Deque in reverse order: P O T P A L