版权声明:本文为博主原创文章。未经博主同意不得转载。 https://blog.csdn.net/u010016150/article/details/32715801

  C++ Prime确实有点难啊!看了好久都没弄清楚,一点点慢慢来。

  1. #include <iostream>
  2. #include <string>
  3. #include <cstdio>
  4. template <class Type> class Queue;
  5. //function template declaration must precede friend declaration in QueueItem
  6. template <class T>
  7. std::ostream& operator<<(std::ostream&,const Queue<T>&);
  8. template <class Type> class QueueItem{
  9. friend class Queue<Type>;
  10. // needs access to item and next
  11. friend std::ostream&
  12. operator<< <Type> (std::ostream&,const Queue<Type>&);
  13. // private class: no public section
  14. QueueItem(const Type &t):item(t),next(0){}
  15. Type item; // value stored in this element
  16. QueueItem *next; // pointer to next element in the Queue
  17. };
  18. template <class Type> class Queue{
  19. // needs access to head
  20. friend std::ostream&
  21. operator << <Type> (std::ostream&,const Queue<Type>&);
  22. public:
  23. // empty Queue
  24. Queue():head(0),tail(0){}
  25. // construct a Queue from a pair of iterators on some sequence
  26. template <class It>
  27. Queue(It beg,It end):
  28. head(0),tail(0){ copy_elems(beg,end); }
  29. // copy control to manage pointers to QueueItems in the Queue
  30. Queue(const Queue &Q)
  31. :head(0),tail(0){ copy_elems(Q); }
  32. Queue& operator=(const Queue&); // left as exercise for the reader
  33. ~Queue() { destroy(); }
  34. // replace current Queue by contents delimited by a pair of iterators
  35. template <class Iter> void assign(Iter,Iter);
  36. // return element from head of Queue
  37. // unchecked operation:front on an empty Queue is undefined
  38. Type& front() {return head->item; }
  39. const Type &front() const {return head->item;}
  40. void push(const Type &);
  41. void pop();
  42. bool empty()const{ //true if no elements in the Queue
  43. return head == 0;
  44. }
  45. private:
  46. QueueItem<Type> *head;
  47. QueueItem<Type> *tail;
  48. void destroy();
  49. void copy_elems(const Queue&);
  50. // version of copyto be used by assign to copy elements from iterator range
  51. template <class Iter> void copy_elems(Iter,Iter);
  52. };
  53. // push 函数
  54. // push 成员将新项放在队列末尾
  55. template <class Type> void Queue<Type>::push(const Type &val)
  56. {
  57. // allocate a new QueueItem object
  58. QueueItem<Type> *pt = new QueueItem<Type>(val);
  59. // put item onto existing(眼下) queue
  60. if(empty())
  61. head = tail = pt; // the queue now has only one element
  62. else {
  63. tail->next = pt; // add new element to end of the queue
  64. tail = pt;
  65. }
  66. }
  67. //copy_element 函数
  68. template <class Type>
  69. void Queue<Type>::copy_elems(const Queue &orig)
  70. {
  71. // copy elements from orig into this Queue
  72. // loop stops when to pt == 0, which happens when we reach orig.tail
  73. for (QueueItem<Type> *pt = orig.head; pt; pt = pt->next)
  74. push(pt->item); // copy element
  75. }
  76. // destroy 函数
  77. template <class Type> void Queue<Type>::destroy()
  78. {
  79. while (!empty())
  80. pop();
  81. }
  82. //pop函数
  83. template <class Type> void Queue<Type>::pop()
  84. {
  85. // pop is unchecked: Popping off an empty Queue is undefined
  86. QueueItem<Type>* p = head;
  87. head = head->next;
  88. delete p;
  89. }
  90. int main()
  91. {
  92. int n,val;
  93. Queue<int> IntQ;
  94. /*Queue<double> DouQ;
  95. Queue<string> StrQ;*/
  96. printf("Please enter you want total number: ");
  97. std::cin>>n;
  98. while(n){
  99. std::cin>>val;
  100. IntQ.push(val);
  101. n--;
  102. }
  103. while(!IntQ.empty()){
  104. std::cout<<IntQ.front()<<std::endl;
  105. IntQ.pop();
  106. }
  107. return 0;
  108. }

STL 队列模板实现的更多相关文章

  1. C++ 标准模板库STL 队列 queue 使用方法与应用介绍

    C++ 标准模板库STL 队列 queue 使用方法与应用介绍 queue queue模板类的定义在<queue>头文件中. 与stack模板类很相似,queue模板类也需要两个模板参数, ...

  2. 模板——STL队列

    C++ STL queue 容器优先队列&&队列 队列 #include<queue> #include<iostream> using namespace s ...

  3. STL标准模板库介绍

    1. STL介绍 标准模板库STL是当今每个从事C++编程的人需要掌握的技术,所有很有必要总结下 本文将介绍STL并探讨它的三个主要概念:容器.迭代器.算法. STL的最大特点就是: 数据结构和算法的 ...

  4. luoguP1886 滑动窗口(单调队列模板题)

    题目链接:https://www.luogu.org/problem/P1886#submit 题意:给定n个数,求大小为k的滑动窗口中最小值和最大值. 思路:单调队列模板题. AC代码: #incl ...

  5. Sliding Window POJ - 2823 单调队列模板题

    Sliding Window POJ - 2823 单调队列模板题 题意 给出一个数列 并且给出一个数m 问每个连续的m中的最小\最大值是多少,并输出 思路 使用单调队列来写,拿最小值来举例 要求区间 ...

  6. STL学习系列一:STL(标准模板库)理论基础

    STL(Standard Template Library,标准模板库)是惠普实验室开发的一系列软件的统称.现然主要出现在C++中,但在被引入C++之前该技术就已经存在了很长的一段时间. STL的从广 ...

  7. STL(标准模板库)理论基础,容器,迭代器,算法

    基本概念 STL(Standard Template Library,标准模板库)是惠普实验室开发的一系列软件的统称.现然主要出现在C++中,但在被引入C++之前该技术就已经存在了很长的一段时间.   ...

  8. STL标准模板类

    STL,中文名标准模板库,是一套C++的标准模板类(是类!),包含一些模板类和函数,提供常用的算法和数据结构. STL分为:迭代器,容器,适配器,算法以及函数对象. --迭代器是一种检查容器内元素并遍 ...

  9. STL(标准模板库)基本概念

    一.什么是STL STL(Standard Template Library,标准模板库)的从广义上讲分为三类:algorithm(算法).container(容器)和iterator(迭代器),容器 ...

随机推荐

  1. Django框架(十六)—— cookie和session组件

    目录 cookie和session组件 一.cookie 1.cookie的由来 2.什么是cookie 3.cookie的原理 4.cookie的覆盖 5.在浏览器中查看cookie 6.cooki ...

  2. 元类,sqlalchemy查询

    import sqlalchemy from sqlalchemy.ext.declarative import declarative_base #创建连接实例 db = sqlalchemy.cr ...

  3. linux性能查看调优

    一 linux服务器性能查看1.1 cpu性能查看1.查看物理cpu个数:cat /proc/cpuinfo |grep "physical id"|sort|uniq|wc -l ...

  4. 数据结构与算法简记--redis有序集合实现-跳跃表

    跳表 定义 为一个值有序的链表建立多级索引,比如每2个节点提取一个节点到上一级,我们把抽出来的那一级叫做索引或索引层.如下图所示,其中down表示down指针,指向下一级节点.以此类推,对于节点数为n ...

  5. Centos7.6安装教程 && history设置显示执行命令的时间

    一.规划磁盘使用空间(磁盘总大小200GB) /dev/sda1 mount /boot 1G mount point /dev/sda2 mount / 100G /dev/sda3 mount / ...

  6. 18-vim-插入命令

    在vi中除了常用的i进入编辑模式外,还提供了以下命令同样可以进入编辑模式: 命令 英文 功能 使用频率 i insert 在当前字符前插入文本 常用 I insert 在行首插入文本 较常用 a ap ...

  7. UltraEdit常用快捷键

    UltraEdit是一套功能强大的文本编辑器,可以编辑文本.十六进制.ASCII码,可以取代记事本,内建英文单字检查.C++及VB指令突显,可同时编辑多个文件,而且即使开启很大的文件速度也不会慢. 说 ...

  8. 自定义DbUtils通用类

    本实例使用C3P0连接池做连接,详见https://www.cnblogs.com/qf123/p/10097662.html开源连接池C3P0的使用 DBUtils.java package com ...

  9. mysql的一些语法

    alter table S61.T6198 modify  F12 varchar(30) DEFAULT NULL COMMENT '流水号'; 修改表字段结构. mysql不支持top ,只支持l ...

  10. stdin stdout stderr - 标准 I/O 流

    Fd #include <stdio.h> Fd extern FILE *stdin; Fd extern FILE *stdout; Fd extern FILE *stderr; D ...