2.6 C++STL queue详解

2022-11-26,,,

文章目录

2.6.1 引入
2.6.2 代码示例
2.6.3 代码运行结果
总结


2.6.1 引入

首先,在STL中 queue 和 stack 其实并不叫容器(container),而是叫适配器(adapter),他们是对容器的再封装。
队列,简称队,是一种操作受限的线性表。限制为:只允许在队首删除(出队),队尾插入(入队),其特点是先进先出。在STL中,queue作为一种适配器,其底层容器一般为deque(双端队列)和list(双向链表),其中deque为默认底层容器。

queue用法学习 C++的STL库常用API–queue

2.6.2 代码示例

#include<iostream>
#include<queue>
using namespace std; void text01()
{
//初始化
queue<int> q;
queue<int> q2(q); //queue操作
q.push(10);
q.push(100);
q.push(1000);
q.push(30);
q.push(20);
cout << "队尾:" << q.back() << endl; //打印队列数据
while (!q.empty())
{
cout << q.front() << " ";//队头
q.pop();//删除队头
}
cout << endl;
cout << "size:" << q.size() << endl;//剩余元素
} int main()
{
cout << "\ntext01\n";
text01();
return 0;
}

2.6.3 代码运行结果

总结

queue为先进先出的适配器,和stack一样,需要好好掌握。


谢谢阅读(〃’ ▽ '〃)如有纰漏欢迎指出,觉得还不错就点个赞吧。

2.6 C++STL queue详解的相关教程结束。

《2.6 C++STL queue详解.doc》

下载本文的Word格式文档,以方便收藏与打印。