雙端隊列或雙頭隊列是序列容器,兩端都有擴展和收縮函數。它們類似於向量,但是在元素的結尾和開始處插入和刪除時效率更高。與向量不同,可能無法保證連續的存儲分配。
雙端隊列::pop_front()
pop_front()函數用於從前麵的雙端隊列彈出或刪除元素。從一開始就從雙端隊列中刪除該值,並將容器大小減小1。
用法:
dequename.pop_front() 參數: No value is needed to pass as the parameter. Result: Removes the value present at the front of the given deque named as dequename
例子:
Input : mydeque = 1, 2, 3 mydeque.pop_front(); Output: 2, 3 Input : mydeque = 3, 4, 1, 7, 3 mydeque.pop_front(); Output: 4, 1, 7, 3
錯誤和異常
- No-Throw-Guarantee-如果引發異常,則容器中沒有任何更改
- 如果雙端隊列為空,則表示未定義的行為。
// CPP program to illustrate
// pop_front() function
#include <iostream>
#include <deque>
using namespace std;
int main()
{
deque<int> mydeque;
mydeque.push_front(3);
mydeque.push_front(2);
mydeque.push_front(1);
//Deque becomes 1, 2, 3
mydeque.pop_front();
//Deque becomes 2, 3
for (auto it = mydeque.begin(); it != mydeque.end(); ++it)
cout << ' ' << *it;
}
輸出:
2 3
應用:使用push_front()函數輸入具有以下編號和順序的空雙端隊列,並打印雙端隊列的反麵。
Input:1, 2, 3, 4, 5, 6, 7, 8 Output:8, 7, 6, 5, 4, 3, 2, 1
// CPP program to illustrate
// application Of pop_front() function
#include <iostream>
#include <deque>
using namespace std;
int main()
{
deque<int> mydeque{}, newdeque{};
mydeque.push_front(8);
mydeque.push_front(7);
mydeque.push_front(6);
mydeque.push_front(5);
mydeque.push_front(4);
mydeque.push_front(3);
mydeque.push_front(2);
mydeque.push_front(1);
//Deque becomes 1, 2, 3, 4, 5, 6, 7, 8
while (!mydeque.empty()) {
newdeque.push_front(mydeque.front());
mydeque.pop_front();
}
for (auto it = newdeque.begin(); it != newdeque.end(); ++it)
cout << ' ' << *it;
}
輸出:
8 7 6 5 4 3 2 1
雙端隊列::pop_back()
pop_back()函數用於從背麵的雙端隊列中彈出或刪除元素。從末尾從雙端隊列中刪除該值,並將容器大小減小1。
用法:
dequename.pop_back() 參數: No value is needed to pass as the parameter. Result: Removes the value present at the end or back of the given deque named as dequename
例子:
Input : mydeque = 1, 2, 3 mydeque.pop_back(); Output: 1, 2 Input : mydeque = 3, 4, 1, 7, 3 mydeque.pop_back(); Output: 3, 4, 1, 7
錯誤和異常
- No-Throw-Guarantee-如果引發異常,則容器中沒有任何更改
- 如果雙端隊列為空,則表示未定義的行為。
// CPP program to illustrate
// pop_back() function
#include <iostream>
#include <deque>
using namespace std;
int main()
{
deque<int> mydeque;
mydeque.push_front(5);
mydeque.push_front(4);
mydeque.push_front(3);
mydeque.push_front(2);
mydeque.push_front(1);
//Deque becomes 1, 2, 3, 4, 5
mydeque.pop_back();
//Deque becomes 1, 2, 3, 4
for (auto it = mydeque.begin(); it != mydeque.end(); ++it)
cout << ' ' << *it;
}
輸出:
1 2 3 4
應用:
使用push_front()函數輸入具有以下編號和順序的空雙端隊列,並打印雙端隊列的反麵。
Input :1, 20, 39, 43, 57, 64, 73, 82 Output:82, 73, 64, 57, 43, 39, 20, 1
// CPP program to illustrate
// application Of pop_back() function
#include <iostream>
#include <deque>
using namespace std;
int main()
{
deque<int> mydeque, newdeque;
mydeque.push_front(82);
mydeque.push_front(73);
mydeque.push_front(64);
mydeque.push_front(57);
mydeque.push_front(43);
mydeque.push_front(39);
mydeque.push_front(20);
mydeque.push_front(1);
//Deque becomes 1, 20, 39, 43, 57, 64, 73, 82
while (!mydeque.empty()) {
newdeque.push_back(mydeque.back());
mydeque.pop_back();
}
for (auto it = newdeque.begin(); it != newdeque.end(); ++it)
cout << ' ' << *it;
}
輸出:
82 73 64 57 43 39 20 1
相關用法
注:本文由純淨天空篩選整理自AyushSaxena大神的英文原創作品 deque::pop_front() and deque::pop_back() in C++ STL。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。