您的位置:首页 > 编程语言 > C语言/C++

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: