C++ Primer ---适配器 queue

本文详细介绍了队列这种先进先出的数据结构,包括其基本概念、常用操作方法如push、pop等,并通过示例展示了如何使用C++标准库中的queue容器适配器实现队列,适用于初学者和开发者快速掌握队列的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一,概述

先进先出的数据结构,底端加入元素,顶端移除元素,类似stack同样不能有遍历行为,没有迭代器。也是以既有容器为底端容器被归类为陪接器(container adapter),默认底端容器为deque。

二,使用

#include <queue>

using namespace std;

三,方法

queue::push( ); //底部插入元素
queue::pop( ); //顶端移除元素
queue::empty( ); //是否为空
queue::back( );
queue::front( );
queue::size( );

四,示例

  1. #include<list>
  2. #include<iostream>
  3. #include<queue>
  4. #include<deque>
  5. usingnamespacestd;
  6. //Usingqueuewithlist
  7. typedeflist<int>INTLIST;
  8. typedefqueue<int,INTLIST>INTQUEUE;
  9. //Usingqueuewithdeque
  10. typedefdeque<constchar*>CHARDEQUE;
  11. typedefqueue<constchar*,CHARDEQUE>CHARQUEUE;
  12. intmain(void)
  13. {
  14. size_tsize_q;
  15. INTQUEUEq;
  16. CHARQUEUEp;
  17. //Insertitemsinthequeue(useslist)
  18. q.push(42);
  19. q.push(100);
  20. q.push(49);
  21. q.push(201);
  22. size_q=q.size();
  23. cout<<"sizeofqis:"<<size_q<<endl;
  24. while(!q.empty())
  25. {
  26. cout<<q.front()<<"";
  27. q.pop();
  28. }
  29. p.push("cat");
  30. p.push("ape");
  31. p.push("dog");
  32. p.push("mouse");
  33. p.push("horse");
  34. cout<<"\n"<<"p.back:"<<p.back()<<endl;//thelastelemet
  35. size_q=p.size();
  36. cout<<"sizeofpis:"<<size_q<<endl;
  37. while(!p.empty())
  38. {
  39. cout<<p.front()<<"";
  40. p.pop();
  41. }
  42. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值