C++ STL 中的 deque front() 和 deque back()


本文旨在展示 C++ STL 中 deque front() 和 deque back() 函式的功能

什麼是雙端佇列

雙端佇列是雙端佇列,也就是一種序列容器,可提供兩端的擴充套件和收縮功能。佇列資料結構允許使用者僅從 END 插入資料,並從 FRONT 刪除資料。讓我們以公交車站的佇列為例,人員只能從 END 排隊,而排在 FRONT 的人則是第一個被帶走的人,而在雙端佇列中,資料可以在兩端插入和刪除。

什麼是 deque front() 函式

front() 函式用於引用雙端佇列的第一個元素。

語法

dequename.front( )

示例

輸入雙端佇列:12 13 14 15 16

輸出新雙端佇列:12

輸入雙端佇列:C A P T U R E

輸出新雙端佇列:C

可遵循的方法

  • 首先宣告雙端佇列

  • 然後列印該雙端佇列。

  • 然後定義 front() 函式。

透過使用如上方法,我們可以獲取雙端佇列的第一個元素。

示例

// C++ code to demonstrate the working of deque front( ) function
#include<iostream.h>
#include<deque.h>
Using namespace std;
int main ( ){
   // initializing the deque
   Deque<int> deque = { 5, 7, 6, 8, 9 };
   // print the deque
   cout<< “ Deque: “;
   for( auto x = deque.begin( ); x != deque.end( ); ++x)
      cout<< *x << “ “;
   // defining the front( ) function
   cout<< deque.front( );
   return 0;
}

輸出

如果我們執行以上程式碼,它將生成以下輸出

Input  – Deque: 5 7 6 8 9
Output – New Deque: 5
Input  – Deque: L O N D O N
Output – New Deque: L

更新於: 26-2 月-2020

360 次瀏覽

開啟你的 事業

透過完成教程獲得認證

立即開始
廣告