STL_queue

简介: STL_queue

STL_queue

queue的基本概念:

一般的queue容器只能队尾进,队首出,双向队列deque那就是另一回事儿了。

queue常用接口

一段代码学会队列的使用:

#include<iostream>
#include<queue>
using namespace std;
// 队列 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()
{
  // Create queue
  // 创建一个队列
  queue<Person>q;
  // Data preparation
  // 准备数据
  Person p1("唐僧", 30);
  Person p2("孙悟空", 1000);
  Person p3("猪八戒", 900);
  Person p4("沙僧", 800);
  // Push
  // 往队列里面添加元素
  q.push(p1);
  q.push(p2);
  q.push(p3);
  q.push(p4);
  cout << "队列大小为:" << q.size() << endl;
  // Judge whether the queue is empty or not, check the opposite head, check the end of the queue, and exit the queue
  // 判断队列是否为空,然后检查对首和队尾,最后退出队列
  while (!q.empty())
  {
    // check the opposite head
    // 查看对首元素
    cout << "对头元素 --- 姓名:" << q.front().m_Name << "年龄:" << q.front().m_Age << endl;
    // check the end of the queue
    // 查看队尾元素
    cout << "队尾元素 --- 姓名:" << q.back().m_Name << "年龄:" << q.front().m_Age << endl;
    cout << endl;
    // 出队列方法
    // out of queue
    q.pop();
  }
  cout << "队列大小为:" << q.size() << endl;
}
int main()
{
  test01();
  return 0;
}

运行结果:

相关文章
|
4月前
|
存储 算法 C语言
【C++】C++ STL探索:Priority Queue与仿函数的深入解析(一)
【C++】C++ STL探索:Priority Queue与仿函数的深入解析
|
4月前
|
C++
【C++】C++ STL探索:Priority Queue与仿函数的深入解析(三)
【C++】C++ STL探索:Priority Queue与仿函数的深入解析
|
4月前
|
编译器 程序员 C++
【C++】C++ STL探索:Priority Queue与仿函数的深入解析(二)
【C++】C++ STL探索:Priority Queue与仿函数的深入解析
|
7月前
|
设计模式 算法 Java
【c++】STL之stack和queue详解
【c++】STL之stack和queue详解
73 1
|
存储 设计模式 C++
C++ STL stack & queue
stack是一种容器适配器,专门用在具有后进先出操作的上下文环境中,其删除只能从容器的一端进行元素的插入与提取操作。
|
9月前
|
编译器 C++ 容器
【STL】stack与queue的底层原理及其实现
【STL】stack与queue的底层原理及其实现
|
9月前
|
存储 C++ 容器
【STL】priority_queue的底层原理及其实现
【STL】priority_queue的底层原理及其实现
|
存储 算法 C++
C++ STL priority_queue
优先队列是一种容器适配器,根据严格的弱排序标准,它的第一个元素总是它所包含的元素中最大的。
|
存储 算法 程序员
stack、queue、priority_queue的使用和简单实现【STL】
stack、queue、priority_queue的使用和简单实现【STL】
76 0
|
存储 算法 C++
C++【STL】之priority_queue学习
C++ STL 优先级队列,常用接口的使用和模拟实现详细讲解,干货满满!
121 1
C++【STL】之priority_queue学习