双端队列或双端队列是序列容器,两端都有扩展和收缩功能。它们与向量类似,但是在元素的结尾和开始处插入和删除时效率更高。与向量不同,可能无法保证连续的存储分配。
deque :: pop_front()
pop_front()函数用于从前面的双端队列中弹出或删除元素。从一开始就从双端队列中删除该值,并将容器大小减小1。
句法 :
dequename.pop_front()
Parameters :
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
#include
using namespace std;
int main()
{
deque 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
#include
using namespace std;
int main()
{
deque 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
deque :: pop_back()
pop_back()函数用于从背面的双端队列中弹出或删除元素。从末尾从双端队列中删除该值,并将容器大小减小1。
句法 :
dequename.pop_back()
Parameters :
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
#include
using namespace std;
int main()
{
deque 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
#include
using namespace std;
int main()
{
deque 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
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。