C++ STL 中的 deque::begin() 和 deque::end()


在本文中,我們將討論 C++ STL 中 deque::begin() 和 deque::end() 函式的工作原理、語法和示例。

什麼是 Deque?

Deque 是雙端佇列,是一種序列容器,可以在兩端進行擴充套件和收縮。佇列資料結構只允許使用者在尾部插入資料,在頭部刪除資料。讓我們以公交站點的佇列為例,乘客只能在佇列尾部加入,而站在佇列頭部的人最先離開;而在雙端佇列中,可以在兩端進行資料的插入和刪除。

什麼是 deque::begin()?

deque::begin() 是 C++ STL 中的一個內建函式,在 `` 標頭檔案中宣告。deque::begin() 返回一個迭代器,它引用與該函式關聯的 deque 容器的第一個元素。begin() 和 end() 都用於迭代 deque 容器。

語法

mydeque.begin();

引數

此函式不接受任何引數。

返回值

它返回一個指向 deque 容器中第一個元素的迭代器。

示例

Input: deque<int> mydeque = {10, 20, 30, 40};
   mydeque.begin();
Output:
   Element at the beginning is =10

示例

線上演示

#include <deque>
#include <iostream>
using namespace std;
int main(){
   deque<int> Deque = {2, 4, 6, 8, 10 };
   cout<<"Elements are : ";
   for (auto i = Deque.begin(); i!= Deque.end(); ++i)
      cout << ' ' << *i;
   return 0;
}

輸出

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

Elements are : 2 4 6 8 10

什麼是 deque::end()?

deque::end() 是 C++ STL 中的一個內建函式,在 `` 標頭檔案中宣告。deque::end() 返回一個迭代器,它引用與該函式關聯的 deque 容器的最後一個元素的下一個位置。begin() 和 end() 都用於迭代 deque 容器。

語法

mydeque.end();

引數

此函式不接受任何引數。

返回值

它返回一個指向 deque 容器中最後一個元素的下一個位置的迭代器。

示例

Input: deque<int> mydeque = {10, 20, 30, 40};
   mydeque.end();
Output:
   Element at the ending is =5 //Random value which is next to the last element.

示例

線上演示

#include <deque>
#include <iostream>
using namespace std;
int main(){
   deque<int> Deque = { 10, 20, 30, 40};
   cout<<"Elements are : ";
   for (auto i = Deque.begin(); i!= Deque.end(); ++i)
      cout << ' ' << *i;
   return 0;
}

輸出

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

Elements are : 10 20 30 40

更新於:2020年3月5日

1K+ 次瀏覽

開啟您的職業生涯

透過完成課程獲得認證

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