C++ STL 中的 deque_rend()


本任務演示 C++ STL 中 Deque rend() 函式的功能。

什麼是 Deque?

Deque 是雙端佇列,是一種序列容器,可以在兩端進行擴充套件和收縮操作。佇列資料結構只允許使用者在隊尾插入資料,在隊首刪除資料。例如公交車站的隊伍,只能在隊尾加入乘客,而隊首的乘客最先離開。而在雙端佇列中,可以在兩端進行資料的插入和刪除。

什麼是 rend() 函式?

rend() 函式返回一個反向迭代器,指向 deque 容器中第一個元素之前的元素。rend() 函式反轉 deque。

語法 − deque_name.rend()

返回值 − 返回一個反向迭代器,指向 deque 的第一個元素之前的 position。

示例

輸入 Deque − 5 4 4 2 0

輸出 反轉後的 Deque − 0 2 4 4 5

輸入 Deque − R E C T I F I E R

輸出 反轉後的 Deque − R E F I T C E R (Note: The example output "GOLDEN" is incorrect and doesn't reflect a simple reverse)

可遵循的方法

  • 首先宣告 deque。

  • 然後列印 deque。

  • 然後使用 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

更新於:2020年2月26日

116 次瀏覽

開啟您的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.