C++ STL 中的 deque::begin() 和 deque::end()
在本文中,我們將討論 C++ STL 中 deque::begin() 和 deque::end() 函式的工作原理、語法和示例。
什麼是 Deque?
Deque 是雙端佇列,是一種序列容器,可以在兩端進行擴充套件和收縮。佇列資料結構只允許使用者在尾部插入資料,在頭部刪除資料。讓我們以公交站點的佇列為例,乘客只能在佇列尾部加入,而站在佇列頭部的人最先離開;而在雙端佇列中,可以在兩端進行資料的插入和刪除。
什麼是 deque::begin()?
deque::begin() 是 C++ STL 中的一個內建函式,在 `
語法
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 中的一個內建函式,在 `
語法
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
廣告
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP