กำหนดให้เป็นหน้าที่แสดงการทำงานของฟังก์ชัน deque push_back( ) ใน C++ STL
เด็คคืออะไร
Deque คือ Double Ended Queues ซึ่งเป็นคอนเทนเนอร์ลำดับที่ให้การทำงานของการขยายและการหดตัวทั้งสองด้าน โครงสร้างข้อมูลคิวอนุญาตให้ผู้ใช้แทรกข้อมูลได้เฉพาะที่ END และลบข้อมูลออกจาก FRONT ลองมาเปรียบเทียบคิวที่ป้ายรถเมล์ที่คนสามารถแทรกลงในคิวจาก END เท่านั้นและบุคคลที่ยืนอยู่ด้านหน้าจะเป็นคนแรกที่ถูกลบในขณะที่ในคิวแบบ Double End การแทรกและการลบข้อมูลทำได้ทั้งสองแบบ จบ.
ฟังก์ชัน deque push_back( ) คืออะไร
ฟังก์ชัน push_back( ) ใช้เพื่อแทรกองค์ประกอบใหม่ลงในจุดสิ้นสุด
ไวยากรณ์
dequename.push_front(value)
พารามิเตอร์
ค่า - กำหนดองค์ประกอบใหม่ที่จะแทรกที่ด้านหลังของ deque
ตัวอย่าง
ป้อนข้อมูล เด็ค − 45 46 47 48 49
ผลผลิต ใหม่ Deque − 45 46 47 48 49 50
ป้อนข้อมูล Deque − B L A N K E T
ผลผลิต ใหม่ Deque − B L A N K E T S
แนวทางสามารถติดตามได้
-
ขั้นแรก เราประกาศเดค
-
จากนั้นเราก็พิมพ์ดีค
-
จากนั้นเรากำหนดฟังก์ชัน push_back( )
โดยใช้วิธีการข้างต้น เราสามารถแทรกองค์ประกอบใหม่ที่ด้านหลังของ deque องค์ประกอบใหม่ควรมีประเภทข้อมูลเหมือนกับ deque
ตัวอย่าง
// C++ code to demonstrate the working of deque push_back( ) function
#include<iostream.h>
#include<deque.h>
Using namespace std;
int main ( ){
// initializing the deque
Deque<int> deque = { 71, 75, 73, 76, 77 };
// print the deque
cout<< “ Deque: “;
for( auto x = deque.begin( ); x != deque.end( ); ++x)
cout<< *x << “ “;
// defining the push_backt( ) function
deque.push_back(78);
// printing new deque after inserting new element
for( x = deque.begin( ); x != deque.end( ); ++x)
cout<< “ “ << *x;
return 0;
} ผลลัพธ์
หากเรารันโค้ดด้านบน มันจะสร้างผลลัพธ์ต่อไปนี้
Input - Deque: 71 75 73 76 77 Output - New Deque:71 75 73 76 77 78 Input – Deque: B R E A K Output – New Deque: B R E A K S
ตัวอย่าง
// C++ code to demonstrate the working of deque push_back( ) function
#include<iostream.h>
#include<deque.h>
Using namespace std;
int main( ){
// initializing deque
deque<int> deque ={ 64, 65, 66, 69, 68 };
cout<< “ Deque: “;
for( auto x = deque.begin( ); x != deque.end( ); ++x)
cout<< *x << “ “;
// defining the push_back( ) function
deque.push_back(67);
// printing new deque after inserting new element
for(x = deque.begin( ); x != deque.end( ); ++x)
cout<< “ “ << *x;
return 0;
} ตัวอย่าง
หากเรารันโค้ดด้านบน มันจะสร้างผลลัพธ์ต่อไปนี้
Input: 64 65 66 69 68 Output: 64 65 66 69 68 67 Input: T U T O R I A L Output: T U T O R I A L S