stack容器,queue容器

目录

1.基本概念

 2.stack常用接口

3.queue容器基本概念

 4.queue容器常用接口,push,pop,back,front,empty,size

1.基本概念

 2.stack常用接口

#include<iostream>
using namespace std;
#include<stack>

//栈常用接口
void test01()
{
	stack<int>s;//需要包含头文件#include<stack>
	
	//入栈
	s.push(10);
	s.push(20);
	s.push(30);
	s.push(40);

	//只要栈不空 查看栈顶 并执行出战
	while (!s.empty())//为空返回真
	{
		//查看栈顶
		cout << s.top() << endl;
		//出栈
		s.pop();
	}
	cout << "栈的大小:" << s.size() << endl;
}

int main()
{
	//随机数种子,通过系统的时间算随机数
	srand((unsigned int)time(NULL));//需要包含ctime头文件
	test01();
	
	system("pause");//按任意键继续
	return 0;
}

3.queue容器基本概念

 

 4.queue容器常用接口,push,pop,back,front,empty,size

#include<iostream>
using namespace std;
#include<queue>
//队列queue
class Person
{
public:
	Person(string name, int age)
	{
		m_Name = name;
		m_Age = age;
	}
	string m_Name;
	int m_Age;
};
void test01()
{
	//创建队列
	queue<Person>q;//需要头文件#include<queue>
	//准备数据
	Person p1("唐僧", 30);
	Person p2("孙悟空", 1000);
	Person p3("猪八戒", 900);
	Person p4("沙僧", 800);
	q.push(p1);
	q.push(p2);
	q.push(p3);
	q.push(p4);
	//判断只要队列不为空 查看队头 队尾,出队
	while (!q.empty())//为空返回真,不为空返回假
	{
		cout <<"队头:"<< q.front().m_Name << " " << q.front().m_Age<< endl;
		cout << "队尾:" << q.back().m_Name << " " << q.back().m_Age << endl;
		q.pop();
	}
	cout << "队列的大小:" << q.size() << endl;
}

int main()
{
	
	test01();
	
	system("pause");//按任意键继续
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值