C++容器--stack,queue
2018-02-11 18:09
281 查看
#include <iostream>
#include <stack>
#include <queue>
using namespace std;
//栈
void func1()
{
stack<int> s;
s.push(1);
s.push(2);
s.push(3);
cout << s.top() << endl;
cout << s.size() << endl;
while(!s.empty())
{
cout << s.top()<< endl;
s.pop();
}
}
//队列
void func2()
{
queue<int> q;
q.push(1);
q.push(4);
q.push(5);
cout << q.front()<< endl;
cout << q.back()<< endl;
while(!q.empty())
{
cout << q.front() << endl;
q.pop();
}
}
class MyQueue
{
public:
void push(int a)
{
s1.push(a);
}
void pop()
{
if(s2.empty())
{
while(s1.size() > 1)
{
s2.push(s1.top());
s1.pop();
}
//从s1 出
if(!s1.empty())
s1.pop();
}
else
{
s2.pop();
}
}
private:
stack<int> s1;
stack<int> s2;
};
int main()
{
func2();
return 0;
}
#include <stack>
#include <queue>
using namespace std;
//栈
void func1()
{
stack<int> s;
s.push(1);
s.push(2);
s.push(3);
cout << s.top() << endl;
cout << s.size() << endl;
while(!s.empty())
{
cout << s.top()<< endl;
s.pop();
}
}
//队列
void func2()
{
queue<int> q;
q.push(1);
q.push(4);
q.push(5);
cout << q.front()<< endl;
cout << q.back()<< endl;
while(!q.empty())
{
cout << q.front() << endl;
q.pop();
}
}
class MyQueue
{
public:
void push(int a)
{
s1.push(a);
}
void pop()
{
if(s2.empty())
{
while(s1.size() > 1)
{
s2.push(s1.top());
s1.pop();
}
//从s1 出
if(!s1.empty())
s1.pop();
}
else
{
s2.pop();
}
}
private:
stack<int> s1;
stack<int> s2;
};
int main()
{
func2();
return 0;
}
相关文章推荐
- C++语法基础--顺序容器(五)--容器适配器-- queue,priority_queue,stack
- C++之STL(十一):容器适配器(stack、 queue 、priority_queue)源码浅析与使用示例
- C++各个容器比较(vector,deque,list,set,map,queue,stack)
- C++容器-4容器适配器stack、queue、priority_queue
- C++各个容器比较(vector,deque,list,set,map,queue,stack)
- C++ STL基本容器的使用(vector、list、deque、map、stack、queue)
- C++ STL queue,priority_queue,stack,容器适配器
- C++ STL容器stack和queue详解
- C++的STL容器之容器适配器:stack、queue、priority_queue
- 从零开始学C++之STL(十一):容器适配器(stack、 queue 、priority_queue)源码浅析与使用示例
- C++:顺序容器及顺序容器适配器(stack、queue等)
- C++各个容器比较(vector,deque,list,set,map,queue,stack)
- C++各个容器比较(vector,deque,list,set,map,queue,stack)
- c++STL容器(map,set,vector,stack,queue)
- 容器适配器(Adapter):stack,queue,priority_queue
- 【C++】队列容器 头文件 #include<queue> 解读
- C++(STL)考点--容器(stack)
- c++stack容器介绍
- C++ STL--stack/queue 的使用方法
- STL--容器适配器(queue、priority_queue、stack)