[C++ Improve Programming] 3.6 queue container

3.6 queue container

3.6.1 Basic Concepts of Queue

Concept: Queue is a first in first out (First In First Out, FIFO) data structure, it has two outlets

The queue container allows adding elements from one end and removing elements from the other end

Only the head and tail of the queue can be used by the outside world, so the queue does not allow traversal behavior

Incoming data in the queue is called- enqueue push

Data out of the queue is called- Dequeue pop

Queue in life:

3.6.2 Queue Common Interface

Function description: Commonly used external interface of stack container


  • queue<T> que; //queue is implemented using template classes, the default structure of the queue object
  • queue(const queue &que); //Copy constructor

Assignment operation:

  • queue& operator=(const queue &que); //Overload the equal sign operator

Data access:

  • push(elem); //Add elements to the end of the team
  • pop(); //Remove the first element from the head of the team
  • back(); //Return to the last element
  • front(); //Return the first element

Size operation:

  • empty(); //Determine whether the stack is empty
  • size(); //Return the size of the stack


#include <queue>
#include <string>
class Person
	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("唐僧", 30);
	Person p2("孙悟空", 1000);
	Person p3("猪八戒", 900);
	Person p4("沙僧", 800);

	//向队列中添加元素  入队操作

	while (!q.empty()) {
		cout << "队头元素-- 姓名: " << q.front().m_Name 
              << " 年龄: "<< q.front().m_Age << endl;
		cout << "队尾元素-- 姓名: " << q.back().m_Name  
              << " 年龄: " << q.back().m_Age << endl;
		cout << endl;

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

int main() {



	return 0;

to sum up:

  • Join the team — push
  • Out of the team — pop
  • Back to the head element — front
  • Back to the tail element — back
  • Determine whether the team is empty — empty
  • Return queue size — size

For more information, please follow the official account:

Guess you like

Origin blog.csdn.net/yegeli/article/details/114442901