STL之queue介绍

版权声明:本文为博主原创文章,商业转载请联系作者获得授权,非商业转载请注明出处。 https://blog.csdn.net/liitdar/article/details/82529350

本文主要介绍 STL 中 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<T> 的第一个和最后一个元素:即只能在容器的末尾添加新元素、或只能从头部移除元素,保持 FIFO(先进先出)原则。

2.1 初始化

queue<int> que_int;

 2.2 成员函数

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

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

2.3 基本操作

向 queue 中添加元素,例如:que_int.push(x); 将 x 插入到队列的末端;

从 queue 中弹出元素,例如:que_int.pop(); 弹出队列 que_int 的第一个元素。需要注意的是,此操作并不会返回被弹出元素的值。

访问 queue 首元素,例如:que_int.front(),即访问最早被压入到队列中的元素。

访问 queue 队尾元素,例如:que_int.back(),即访问最后被压入到队列中的元素。

判断 queue 队列是否为空,例如:que_int.empty()。当队列为空时,返回 true。

获取队列中的元素个数,例如:que_int.size()。

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

queue<int> que_int;
que_int = queue<int>();

3 示例代码

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

#include <queue>
#include <iostream>

using namespace std;

int main()
{
    queue<int> que_int;

    // 在队列末尾依次插入11,12,13
    que_int.push(11);
    que_int.push(12);
    que_int.push(13);

    // 获取队列中的最后一个元素
    int nValue1 = que_int.back();
    cout << "The last value of queue is: " << nValue1 << endl;

    // 获取队列中的第一个元素
    int nValue2 = que_int.front();
    cout << "The first value of queue is: " << nValue2 << endl;

    // 弹出队列中的第一个元素,之后再获取队列中的第一个元素
    que_int.pop();
    nValue2 = que_int.front();
    cout << "After pop, the first value of queue is: " << nValue2 << endl;

    // 返回队列中的元素个数
    int nSize = que_int.size();
    cout << "The size of queue is: " << nSize << endl;

    // 判断队列是否为空
    bool bFlag = que_int.empty();
    if (bFlag)
    {
        cout << "queue is empty." << endl;
    }
    else
    {
        cout << "queue is not empty." << endl;
    }
    
    // 清空队列
    que_int = queue<int>();

    // 判断队列是否为空
    bFlag = que_int.empty();
    if (bFlag)
    {
        cout << "After clear, queue is empty." << endl;
    }
    else
    {
        cout << "After clear, queue is not empty." << endl;
    }

    return 0;
}

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

 

猜你喜欢

转载自blog.csdn.net/liitdar/article/details/82529350
今日推荐