首页 > 代码库 > 双端队列
双端队列
//插入元素//
#include<iostream> #include<deque> using namespace std; int main() { deque<int> d; d.push_back(1);//尾部插入 d.push_back(2); d.push_back(3); d.push_front(10);//d.insert(d.begin()+1, 10);头部插入 d.push_front(20);//d.insert(d.begin()+2, 20); cout<<d[0]<<" "<<d[1]<<" "<<d[2]<<endl; return 0; }
//遍历//
#include<iostream> #include<deque> using namespace std; int main() { deque<int> d; d.push_back(1); d.push_back(2); d.push_back(3); for(int i = 0; i < d.size(); i ++) cout<<d[i]<<" "; cout<<endl; deque<int>::iterator it; for(it = d.begin(); it != d.end(); it ++) cout<<*it<<" "; cout<<endl; deque<int>::reverse_iterator rit; for(rit = d.rbegin(); rit != d.rend(); rit ++) cout<<*rit<<" ";//反向遍历 cout<<endl; return 0; }
//删除//
#include<iostream> #include<deque> using namespace std; int main() { deque<int> d; for(int i = 1; i < 6; i ++) d.push_back(i); d.pop_front(); d.pop_front(); deque<int>::iterator it; for(it = d.begin(); it != d.end(); it ++) cout<<*it<<" "; cout<<endl; d.pop_back(); for(it = d.begin(); it != d.end(); it ++) cout<<*it<<" "; cout<<endl; d.erase(d.begin()+1); for(it = d.begin(); it != d.end(); it ++) cout<<*it<<" "; cout<<endl; d.clear(); cout<<d.size()<<endl; return 0; }
双端队列
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。