C++编程语言STL之queue介绍

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

1 概述

适配器(adaptor)是STL中的一个通用概念。容器、迭代器和函数都有适配器。本质上,适配器是一种机制,它能使某种事物的行为看起来像另外一种事物。容器适配器接受一种已有的容器类型,使其行为看起来像另外一种不同的(容器)类型。

queue就是STL定义的一种顺序容器适配器,其中的数据是以FIFO(First In First Out)的方式组织的。

引用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(初始化)

可以采用下面的方法构造queue(以元素为int类型为例):

queue<int> intQueue;

2.2 推入数据至queue末尾

向queue中添加元素,将元素推入queue末尾,用法示例如下:

int element = 1;
intQueue.push(element);

2.3 判断queue是否为空

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

intQueue.empty();

2.4 返回queue首元素

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

int value = intQueue.front();

2.5 弹出queue首元素

弹出queue首元素的方法如下(需要注意的是,此操作并不会返回被弹出元素的值):

intQueue.pop();

2.6 返回queue队尾元素

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

intQueue.back();

2.7 获取queue中元素个数

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

intQueue.size();

2.8 清空队列

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

queue<int> intQueue;
intQueue = 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;
}

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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

liitdar

赠人玫瑰,手有余香,君与吾共勉

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值