stl_stack_queue的使用
#include <iostream>
#include <stack>
#include <queue>
using namespace std;
void test_stack()
{
stack<int> st;
st.push(1);
st.push(2);
st.push(3);
st.push(4);
st.push(5);
while (!st.empty())
{
cout << st.top() << endl;
st.pop();
}
}
void test_queue()
{
queue<int> q;
q.push(1);
q.push(2);
q.push(3);
q.push(4);
q.push(5);
while (!q.empty())
{
cout << q.front() << endl;
q.pop();
}
}
int main()
{
test_stack();
cout << endl;
test_queue();
return 0;
}
相关OJ题
class MinStack
{
public:
MinStack()
{}
void push(int val)
{
_st.push(val);
if (_minst.empty() || val<=_minst.top())
_minst.push(val);
}
void pop()
{
if (_minst.top()==_st.top())
_minst.pop();
_st.pop();
}
int top()
{
return _st.top();
}
int getMin()
{
return _minst.top();
}
private:
stack<int> _st;
stack<int> _minst;
};
class Solution
{
public:
bool IsPopOrder(vector<int> pushV,vector<int> popV)
{
stack<int> st;
int popi = 0;
for (auto pushVal : pushV)
{
st.push(pushVal);
while (!st.empty() && popV[popi]==st.top())
{
++popi;
st.pop();
}
}
return st.empty();
}
};