STL专项:deque 双端队列

deque

dequequeue 的升级版,全称为 double-ended queue ,队头和队尾都支持入队和出队,同时还支持遍历,所有操作时间复杂度均为O(1)

声明

deque<int> dq;

常用操作

dq.push_front(x);//在队头插入元素

dq.push_back(x);//在队尾插入元素

dq.front();//获取队头元素

dq.back();//获取队尾元素

//获取队列大小

dq.size();

//判断队列是否为空

dq.empty();

//以下两个操作注意判断队列非空

dq.pop_front();//弹出队头

dq.pop_back();//弹出队尾

遍历deque

//用迭代器遍历

for(auto it = dq.begin(); it != dq.end(); it ++){

cout << *it << ' ';

}

//用基于范围的for循环

for(const auto &val : dq) cout << val << ' ';

一起写论文

一起写论文 | 星码StarryCoding 算法竞赛新手村

代码

删除前一定要记得判空!!!

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;

void solve(){
	int N;cin>>N;
	deque<string> dq;
	for(int i=1;i<=N;i++){
		char a,b;cin>>a>>b;
		if(b=='w'){
			string s;cin>>s;
			if(a=='f'){
				dq.push_front(s);
			}else{
				dq.push_back(s);
			}
		}else if(b=='d'){
			if(!dq.size()) continue;
			if(a=='f') dq.pop_front();
			else dq.pop_back();
		}
	}
	if(dq.empty()) cout<<'#';
	else{
		while(dq.size()){
			cout<<dq.front();
			dq.pop_front();
		}
	}
}

int main(){
	int _;cin>>_;
	while(_--) solve();
	return 0;
}
相关推荐
端平入洛7 小时前
delete又未完全delete
c++
端平入洛1 天前
auto有时不auto
c++
郑州光合科技余经理2 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
feifeigo1232 天前
matlab画图工具
开发语言·matlab
dustcell.2 天前
haproxy七层代理
java·开发语言·前端
norlan_jame2 天前
C-PHY与D-PHY差异
c语言·开发语言
哇哈哈20212 天前
信号量和信号
linux·c++
多恩Stone2 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc
QQ4022054962 天前
Python+django+vue3预制菜半成品配菜平台
开发语言·python·django
遥遥江上月2 天前
Node.js + Stagehand + Python 部署
开发语言·python·node.js