C++ STL 中的 deque push_back( )
本任務演示 C++ STL 中 deque push_back( ) 函式的功能。
什麼是 Deque?
Deque 是雙端佇列,它是一種序列容器,可以在兩端進行擴充套件和收縮操作。佇列資料結構只允許使用者在末尾插入資料,並在開頭刪除資料。讓我們以公交車站的佇列為例:只能在佇列末尾新增人,而排在最前面的人是第一個被移除的人;而在雙端佇列中,可以在兩端插入和刪除資料。
什麼是 deque push_back( ) 函式?
push_back( ) 函式用於在 deque 的末尾插入新元素。
語法
dequename.push_front(value)
引數
value − 定義要插入到 deque 末尾的新元素。
示例
輸入 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
可採用的方法
首先,我們宣告 deque。
然後,我們列印 deque。
然後,我們定義 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
廣告
資料結構
網路
關係型資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP