C++ 队列函数queue用法

C++ queue(队列)提供了队列的全部功能,换句话说就是这里面已经实现了一个先进先出的数据结构。不需要我们再去重新定义各种函数,简化开发过程。
c++ 队列queue的头文件书写格式为:

#include <queue>

实例化形式如下:

queue<ElemType> QueueName;

其中成员函数如下:

1、检验队列是否为空

empty() 堆栈为空则返回真
形式如下:

QueueName.empty();

2、返回队首元素
front() 返回队首元素
形式如下:

QueueName.front();

3、返回队尾元素
back()返回队尾元素
形式如下:

QueueName.back() ;

4、弹出队首元素
pop() 移除队列中最靠前位置的元素,是没有返回值的void函数
形式如下:

QueueName.pop();

5、插入元素
push() 在队尾插入一个元素
形式如下:

QueueName.push(ElemType);

6、栈中数据的数量
size() 返回队列中元素数目
形式如下:

QueueName.size();

例子如下

#include <iostream>
#include <queue>
#include <stdlib.h>

using namespace std;

int main()
{
    queue<int> Queue;
    Queue.push(1);
    Queue.push(2);
    Queue.push(3);
    cout << "the front of the queue is:" << Queue.front() << endl;
    cout << "the size of the queue is:" << Queue.size() << endl;
    cout << "whether the queue is empty(1:yes 0:not):" << Queue.empty() << endl;
    Queue.pop();
    cout << "the front of the queue is:" << Queue.front() << endl;
    cout << "the back of the queue is:" << Queue.back() << endl;
    Queue.pop();
    cout << "the front of the queue is:" << Queue.front() << endl;
    Queue.pop();
    cout << "whether the queue is empty(1:yes 0:not):" << Queue.empty() << endl;
    system("pause");
}

结果如下
在这里插入图片描述

  • 10
    点赞
  • 63
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值