กำหนดให้เป็นหน้าที่แสดงการทำงานของฟังก์ชัน Deque Rend( ) ใน C++ STL
เด็คคืออะไร
Deque คือ Double Ended Queues ซึ่งเป็นคอนเทนเนอร์ลำดับที่ให้การทำงานของการขยายและการหดตัวทั้งสองด้าน โครงสร้างข้อมูลคิวอนุญาตให้ผู้ใช้แทรกข้อมูลได้เฉพาะที่ END และลบข้อมูลออกจาก FRONT ลองมาเปรียบเทียบคิวที่ป้ายรถเมล์ที่คนสามารถแทรกลงในคิวจาก END เท่านั้นและบุคคลที่ยืนอยู่ด้านหน้าจะเป็นคนแรกที่ถูกลบในขณะที่ในคิวแบบ Double End การแทรกและการลบข้อมูลทำได้ทั้งสองแบบ จบ.
ฟังก์ชัน Rend( ) คืออะไร
ฟังก์ชัน rend( ) คืนค่าตัววนซ้ำย้อนกลับที่ชี้ไปยังองค์ประกอบที่อยู่ก่อนหน้าองค์ประกอบแรกในคอนเทนเนอร์ deque ฟังก์ชัน rend( ) จะย้อนกลับ deque
ไวยากรณ์ − deque_name.rend( )
ผลตอบแทน − ส่งกลับตัววนซ้ำย้อนกลับซึ่งชี้ไปยังตำแหน่งก่อนองค์ประกอบแรกของ deque
ตัวอย่าง
ป้อนข้อมูล เด็ค − 5 4 4 2 0
ผลผลิต ย้อนกลับ Deque − 0 2 4 4 5
ป้อนข้อมูล Deque − R E C T I F I E R
ผลผลิต Reversed Deque − G O L D E N
แนวทางสามารถติดตามได้
-
ขั้นแรก เราประกาศเดค
-
จากนั้นเราก็พิมพ์ดีค
-
จากนั้นเราใช้ฟังก์ชันrend( )
-
จากนั้นเราพิมพ์ deque ใหม่หลังจากการย้อนกลับ
โดยใช้วิธีการข้างต้น เราจะได้ deque ที่กลับกัน
ตัวอย่าง
// C++ code to demonstrate the working of deque rend( ) function
#include<iostream.h>
#include<deque.h>
Using namespace std;
int main ( ){
// initializing the deque
Deque<int> deque = { 7, 4, 0, 3, 7 };
// print the deque
cout<< “ Deque: “;
for( auto x = deque.begin( ); x != deque.end( ); ++x)
cout<< *x << “ “;
// printing reverse deque
cout<< “ Reversed deque: ”;
for( x = deque.rbegin( ) ; x != deque.rend( ); ++x)
cout<< “ “ <<*x;
return 0;
} ผลลัพธ์
หากเรารันโค้ดด้านบน มันจะสร้างผลลัพธ์ต่อไปนี้
Input - Deque: 7 4 0 3 7 Output - Reversed Deque: 7 3 0 4 7
ตัวอย่าง
// C++ code to demonstrate the working of deque rend( ) function
#include<iostream.h>
#include<deque.h>
Using namespace std;
int main( ){
// initializing deque
deque<char> deque ={ ‘S’ , ‘U’ , ‘B’ , ‘T’ , ‘R’ , ‘A’ , ‘C’ , ‘T’ };
cout<< “ Deque: “;
for( auto x = deque.begin( ); x != deque.end( ); ++x)
cout<< *x << “ “;
// printing reversed deque
cout<< “ Reversed deque:”;
for( x = deque.rbegin( ) ; x != deque.rend( ); ++x)
cout<< “ “ <<*x;
return 0;
} ผลลัพธ์
หากเรารันโค้ดด้านบน มันจะสร้างผลลัพธ์ต่อไปนี้
Input – Deque: S U B T R A C T Output – Reversed deque : T C A R T B U S