6、queue容器

queue<int>que;

que.push()

que.pop()

que.front()

que.back()

que.empty()

que.size()

------------------------------------

queue:先进先出

只允许从一端新增数据,一端移除数据

队列不允许遍历,只有队头队尾

____________________

push(elem)         pop() 

front()                  back()

____________________

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

void test01()
{
	queue<int>que;

	que.push(10);
	que.push(20);
	que.push(30);
	que.push(40);
	que.push(40);

	cout << "队列大小:" << que.size() << endl;

	while (!que.empty())
	{
		cout << "队头:" << que.front() << endl;
		cout << "队尾:" << que.back() << endl;
		que.pop();

		cout << endl;
	}

	cout << "全部出队后队列的大小:" << que.size() << endl;
}

int main()
{
	test01();

	system("pause");
	return 0;
}

2-person

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

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

void test01()
{
	queue<Person>que;

	Person p1("甄嬛", 15);
	Person p2("宜修", 28);
	Person p3("雍正", 44);
	Person p4("允礼", 25);
	Person p5("沈眉庄", 16);
	Person p6("安陵容", 15);

	que.push(p1);
	que.push(p2);
	que.push(p3);
	que.push(p4);
	que.push(p5);
	que.push(p6);

	cout << que.size() << endl;

	while (!que.empty())
	{
		cout << "队头  姓名:" << que.front().m_Name << "年龄:" << que.front().m_Age << endl;

		cout << "队尾  姓名:" << que.back().m_Name << "年龄:" << que.back().m_Age << endl;
		
		que.pop();

		cout << endl;
	}

	cout << que.size() << endl;
}

int main()
{
	test01();

	system("pause");
	return 0;
}

上一篇:leetcode 二叉树的锯齿形层序遍历


下一篇:C++学习 --- STL常用容器之queue容器