特殊容器: stack 栈 queue 队列 priority_queue优先队列~ 特殊容器没有迭代器的哟~~
1. stack 栈 默认由deque实作而成~ 主要是用 push() pop() top() 三个成员函数
/*
author: wzy1222 ;
email: 627440781@qq.com
for: stack
*/
#include <iostream>
#include <stack>
#include <deque>
#include <vector>
using namespace std;
int main()
{
int arr[]={1,2,3,4,5};
deque<int> ideq(arr,arr+5);
// stack 是由 deque 实作而来,初始化可以直接赋值~ vector就不行了~
stack<int> stk(ideq);
cout<< stk.top() <<endl;
// stack由什么实作而成,就可以由什么来初始化~
vector<int> ivec(arr,arr+5);
stack<int,vector<int> > stk22(ivec);
cout<<stk22.top()<<endl;
stk.push(100);
stk.push(200);
stk.top()=1000;
while( !stk.empty())
{
cout<<stk.top()<<endl;
stk.pop();
}
system("PAUSE");
return 0;
}
2 queue 队列 由deque实作而成 主要有 push() pop() back() front() 四个成员函数
/*
author: wzy1222 ;
email: 627440781@qq.com
for: queue
*/
#include <iostream>
#include <deque>
#include <queue>
using namespace std;
int main()
{
int arr[]={1,2,3,4,5};
deque<int> dqe(arr,arr+5);
// queue 也是由deque实作而成~~
queue<int> que(dqe);
cout<<"back: "<<que.back()<<endl;
cout<<"front "<<que.front()<<endl;
que.push(100);
cout<<"push 100: "<<"que.back()= "<<que.back()<<endl;
que.pop();
cout<<"que.pop "<<"que.front()= "<<que.front()<<endl;
system("PAUSE");
return 0;
}
3 priority_queue 优先队列 由vector实作而成,主要有 push() top() pop()成员函数
/*
author: wzy1222 ;
email: 627440781@qq.com
for: priority_queue
*/
#include <iostream>
#include <queue>
using namespace std;
int main()
{
priority_queue<double> pque;
pque.push(50);
pque.push(100);
pque.push(25);
cout<<"top = "<< pque.top()<<endl;
cout<<endl;
while( !pque.empty())
{
cout<<pque.top()<<endl;
pque.pop();
}
system("PAUSE");
return 0;
}