C++ STL中queue的相关函数与使用

一、queue

queue是STL中现成的队列容器,我们只需要了解他的相关函数及使用方法,即可很方便的帮助我们使用队列这个数据结构。

1.头文件:
#include < queue >

2.变量声明:
queue < 数据类型 >变量名
例queue < int >a; 即声明了一个int型的队列a
同理,也可声明queue < string > b; queue < struct node > c;

3.相关函数:
(1)q.push(x) :即向队列q的队尾插入数据x

#include<queue>
queue<int> q;
q.push(5);
cout << q.front() << endl;

(2)q.front():此函数没有参数,是指返回队首的值(只返回此值,并不从队中删除)

queue<string> q;
q.push("hello");
q.push("world");
cout << q.front() << endl;

(3)q.pop():此函数没有参数,指删除队首元素

queue<double> q;
q.push(5.3);
q.push(8);
cout << q.front() << '\n';
q.pop();
cout << q.front() << '\n';

(4)q.back():此函数没有参数,返回队尾元素的值

queue<double> q;
q.push(5.3);
q.push(8);
cout << q.back() << endl;

(5)q.empty():此函数没有参数,判断队列是否为空,若队空,则返回真(1)

queue<int> q;
cout << q.empty() << endl;
q.push("3");
cout << q.empty() << endl;

(6)q.size():此函数没有参数,返回队列元素个数

queue<double> q;
q.push(5.3);
q.push(8);
cout << q.size() << '\n';
二、priority_queue

关于优先队列priority_queue的使用方法见这里

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值