书上所说的容器适配器常见的有三种
1 stack 堆栈适配器 ( 可用的容器类型 vector deque list)
2 queue 队列适配器 ( 可用的容器类型 deque list)
3 priority_queue 优先级队列 (可用的容器类型 deque vector)
都有 增加--push()和 删除--pop()方法
stack 用法:
#include <iostream>
#include <stack> //堆栈适配器头文件
#include <list>
using namespace std;
int main(void)
{
std::stack< int,std::list<int> > charStack;
cout<<"入栈:"<<endl;
for(int i=0;i<10;i++)
{
cout<<i+66<<endl;
charStack.push(66+i);
}
cout<<"出栈:"<<endl;
int size=charStack.size();
for (i=0;i<size;i++)
{
cout<<charStack.top()<<endl;
charStack.pop();
}
cout<<endl;
return 0;
}
queue用法:
#include <iostream>
#include <queue>
#include <list>
using namespace std;
int main()
{
std::queue< int,list<int> > intQueue;
cout<<"入队:"<<endl;
for(int i=1;i<=10;i++)
{
intQueue.push(i*100);
cout<<i*100<<endl;
}
cout<<"出队:"<<endl;
int size=intQueue.size();
for(i=0;i<size;i++)
{
cout<<intQueue.front()<<endl;
intQueue.pop();
}
return 0;
}
priority_queue 用法:
#include <iostream>
#include <queue>
#include <list>
using namespace std;
int main()
{
std::priority_queue< int,vector<int>,std::greater<int> > intPQueue; //优先级greater<type> 可换成 less<type>
intPQueue.push(100);
intPQueue.push(500);
intPQueue.push(600);
intPQueue.push(200);
intPQueue.push(300);
intPQueue.push(400);
int size=intPQueue.size();
for(int i=0;i<size;i++)
{
cout<<intPQueue.top()<<endl;
intPQueue.pop();
}
return 0;
}