双端队列或双头队列是序列容器,两端都有扩展和收缩函数。它们类似于向量,但是在元素的结尾和开始处插入和删除时效率更高。与向量不同,可能无法保证连续的存储分配。
双端队列::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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。