C++ 提高教程 STL queue容器(队列)

 

 

 

# include<iostream>
# include<queue>
# include<string>
# include<ctime>
using namespace std;


class Person
{
public:
	Person(int age, string name)
	{
		this->m_Age = age;
		this->m_Name = name;
	}
	int m_Age;
	string m_Name;

};

void setPersonInf(queue<Person> &p)
{
	string name = "abcde";
	int age;
	for (int i = 0; i < 5; i++)
	{
		string a = "平民";
		a += name[i];
		int b = rand() % 100;
		Person p1(b,a);
		p.push(p1);
	}
}

void test02()
{
	queue<Person> q;
	setPersonInf(q);
	while (!q.empty())
	{
		cout << "队尾元素为:" << endl;
		cout << "姓名:" << q.back().m_Name << "年龄:" << q.back().m_Age << endl;
		cout << "队头元素为:" << endl;
		cout << "姓名:" << q.front().m_Name << "年龄:" << q.front().m_Age << endl;
		q.pop();
	
	}
}
void test01()
{
	queue<int>q;
	q.push(10);
	q.push(20);
	q.push(30);
	q.push(40);
	while ((!q.empty()))
	{
		cout << "队头元素为:" << q.front() << endl;
		cout << "队尾元素为:" << q.back() << endl;
		//q.pop();
		q.pop();
	}
	cout << "stack的大小" << q.size() << endl;
}
int main()
{
	test02();
	system("pause");
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值