STL中queue(队列)介绍

- 日理万妓 2022-05-14 13:05 335阅读 0赞

本文主要介绍 C++ 编程语言的 STL(Standard Template Library) 中 queue(队列)的相关知识,同时通过示例代码介绍 queue 的使用方法。

1 概述

引用 queue 的官方描述,内容如下:

FIFO queue
queues are a type of container adaptor, specifically designed to operate in a FIFO context (first-in first-out), where elements are inserted into one end of the container and extracted from the other.

queues are implemented as containers adaptors, which are classes that use an encapsulated object of a specific container class as its underlying container, providing a specific set of member functions to access its elements. Elements are pushed into the “back” of the specific container and popped from its “front”.

The underlying container may be one of the standard container class template or some other specifically designed container class. This underlying container shall support at least the following operations:

  • empty
  • size
  • front
  • back
  • push
  • pop

The standard container classes deque and list fulfill these requirements. By default, if no container class is specified for a particular queue class instantiation, the standard container deque is used.

2 用法介绍

对于 queue 来说,只能访问 queue 的第一个和最后一个元素:即只能在容器的末尾添加新元素、或从头部移除元素,保持 FIFO(先进先出)原则。

2.1 初始化

  1. queue<int> que_int;

2.2 成员函数

STL 队列 queue 类成员函数信息如下:

  • back():返回最后一个元素
  • front():返回第一个元素
  • pop():删除第一个元素
  • push():在末尾加入一个元素
  • empty():如果队列为空,则返回真
  • size():返回队列中元素的个数

2.3 基本操作

向 queue 中添加元素,将 x 插入到队列的末端,用法示例如下:

  1. que_int.push(x);

从 queue 中弹出元素,弹出队列 que_int 的第一个元素。需要注意的是,此操作并不会返回被弹出元素的值。用法示例如下:

  1. que_int.pop();

访问 queue 首元素,即访问最早被压入到队列中的元素。用法示例如下:

  1. que_int.front();

访问 queue 队尾元素,即访问最后被压入到队列中的元素。用法示例如下:

  1. que_int.back();

判断 queue 队列是否为空,当队列为空时,返回 true。用法示例如下:

  1. que_int.empty();

获取队列中的元素个数。用法示例如下:

  1. que_int.size();

queue 队列没有 clear() 操作,可以通过“使用空的队列对象赋值”的方法清空队列,用法示例如下:

  1. queue<int> que_int;
  2. que_int = queue<int>();

3 示例代码

这里展示一份队列的示例代码(queue_test1.cpp),内容如下:

  1. #include <queue>
  2. #include <iostream>
  3. using namespace std;
  4. int main()
  5. {
  6. queue<int> que_int;
  7. // 在队列末尾依次插入11,12,13
  8. que_int.push(11);
  9. que_int.push(12);
  10. que_int.push(13);
  11. // 获取队列中的最后一个元素
  12. int nValue1 = que_int.back();
  13. cout << "The last value of queue is: " << nValue1 << endl;
  14. // 获取队列中的第一个元素
  15. int nValue2 = que_int.front();
  16. cout << "The first value of queue is: " << nValue2 << endl;
  17. // 弹出队列中的第一个元素,之后再获取队列中的第一个元素
  18. que_int.pop();
  19. nValue2 = que_int.front();
  20. cout << "After pop, the first value of queue is: " << nValue2 << endl;
  21. // 返回队列中的元素个数
  22. int nSize = que_int.size();
  23. cout << "The size of queue is: " << nSize << endl;
  24. // 判断队列是否为空
  25. bool bFlag = que_int.empty();
  26. if (bFlag)
  27. {
  28. cout << "queue is empty." << endl;
  29. }
  30. else
  31. {
  32. cout << "queue is not empty." << endl;
  33. }
  34. // 清空队列
  35. que_int = queue<int>();
  36. // 判断队列是否为空
  37. bFlag = que_int.empty();
  38. if (bFlag)
  39. {
  40. cout << "After clear, queue is empty." << endl;
  41. }
  42. else
  43. {
  44. cout << "After clear, queue is not empty." << endl;
  45. }
  46. return 0;
  47. }

编译并执行上述代码,结果如下:

70

发表评论

表情:
评论列表 (有 0 条评论,335人围观)

还没有评论,来说两句吧...

相关阅读

    相关 [转]: STL priority_queue 优先队列

    刚开始学习算法不久,一些常用的算法工具还没有掌握,真是丢人! 前一段时间用到优先级队列时,都是自己手动通过最大堆或者最小堆来写一个,容易出错且耗时。接触到STL后,开始用ma