- 栈(后进先出)和队列(先进先出)
- 入栈push,出栈pop,返回栈顶top,判断是否为空empty,返回栈大小size
- 入队push,出队pop,返回队头front,返回队尾back,栈空empty,大小size
#include<iostream>
#include<string>
#include<vector>
#include<deque>
#include<stack>
#include<queue>
#include<algorithm>
using namespace std;
void test01()
{
stack<int>s;
s.push(10);
s.push(20);
s.push(30);
while (!s.empty())
{
cout << "栈顶元素: " << s.top() << endl;
s.pop();
}
cout << "栈的大小为" << s.size() << endl;
}
class Person
{
public:
Person(string name, int age) :mname(name), mage(age) {}
string mname;
int mage;
};
void test02()
{
queue<Person> q;
Person p1("唐僧", 30);
Person p2("孙悟空", 230);
Person p3("沙僧", 130);
Person p4("猪八戒", 300);
q.push(p1);
q.push(p2);
q.push(p3);
q.push(p4);
cout << "当前队列大小: " << q.size() << endl;
while (!q.empty()) {
cout << "当前队头元素姓名 " << q.front().mname << " 年龄: " << q.front().mage << endl;
cout << "当前队尾姓名 " << q.back().mname << " 年龄: " << q.back().mage << endl;
q.pop();
}
cout << "当前队列大小: " << q.size() << endl;
}
int main()
{
test01();
test02();
return 0;
}