01 stack容器和queue容器
#include<iostream>
using namespace std;
#include<stack>
void test1()
{
//特点:符合先进后出数据结构
stack<int> s;
s.push(10);
s.push(20);
s.push(30);
s.push(40);
cout << "栈大小为:" << s.size() << endl;
//只有栈不为空,查看栈顶,并且进行出栈操作
while (!s.empty())
{
//查看栈顶元素
cout << s.top() << endl;
//出栈
s.pop();
}
cout << "出栈后,栈大小为:" << s.size() << endl;
}
int main()
{
test1();
system("pause");
return 0;
}
//总结
//入栈--push
//出栈--pop
//返回栈顶--top
//判断是否为空 --empty
//返回栈大小 --size
#include<iostream>
using namespace std;
#include<queue>
class Person
{
public:
Person(string name, int age)
{
this->m_name = name;
this->m_age = age;
}
string m_name;
int m_age;
};
void test01()
{
queue<Person> q;
Person p1("唐僧", 40);
Person p2("孙悟空", 970);
Person p3("猪八戒", 860);
Person p4("沙僧", 465);
q.push(p1);
q.push(p2);
q.push(p3);
q.push(p4);
cout << "队列大小为:" << q.size() << endl;
while (!q.empty())
{
cout << "队头,姓名:" << q.front().m_name << " 年龄" << q.front().m_age << "\t";
cout << "队尾,姓名:" << q.back().m_name << " 年龄" << q.back().m_age << endl;
q.pop();
}
cout << "出队后,队列大小为:" << q.size() << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
//总结
//入队--push
//出队--pop
//返回队头元素--front
//返回队尾元素--back
//判断队是否为空--empty
//返回队列大小--size
评论列表 (0 条评论)