stack,deque,queue对照

stack,deque,queue对比

stack堆栈,没有迭代器,支持push()方法。后进先出,top()返回最顶端的元素,pop()剔除最顶元素

deque双端队列,支持迭代器,有push_back()方法,跟vector差不多,比vector多了个pop_front,push_front方法

queue队列,先进先出,不支持迭代器,有push()方法,pop()剔除第一个元素,front()返回第一个元素

代码如下:

#include<iostream>
#include<stack>
#include<string>
using namespace std;
int main(){
 stack<int>s;
 for(int i=1;i<=10;++i){
  s.push(i);
 }
 for(int j=0;j<10;++j){
  cout<<s.top()<<" ";
  s.pop();
 }
 cout<<endl;
 system("pause");
 return 0;
}

stack,deque,queue对照


#include<iostream>
#include<string>
#include<deque>
using namespace std;
int main(){
 deque<int>q;
 for(int i=0;i<10;++i){
  q.push_back(i);
 }
 cout<<q.front()<<endl;
 for(deque<int>::iterator iter=q.begin();iter!=q.end();++iter){
  cout<<*iter<<" ";
 }
 cout<<endl;
 cout<<q.back()<<endl;
 system("pause");
 return 0;
}

stack,deque,queue对照


#include<iostream>
#include<queue>
#include<string>
using namespace std;
int main(){
 queue<int>q;
 for(int i=0;i<10;++i){
  q.push(i);
 }
 for(int i=0;i<10;++i){
  cout<<q.front()<<" ";
  q.pop();
 }
 cout<<endl;
 system("pause");
 return 0;
}

stack,deque,queue对照