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

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

#include <iostream>
#include <string>
#include <cstdio> template <class Type> class Queue; //function template declaration must precede friend declaration in QueueItem
template <class T>
std::ostream& operator<<(std::ostream&,const Queue<T>&); template <class Type> class QueueItem{
friend class Queue<Type>;
// needs access to item and next
friend std::ostream&
operator<< <Type> (std::ostream&,const Queue<Type>&);
// private class: no public section
QueueItem(const Type &t):item(t),next(0){}
Type item; // value stored in this element
QueueItem *next; // pointer to next element in the Queue
}; template <class Type> class Queue{
// needs access to head
friend std::ostream&
operator << <Type> (std::ostream&,const Queue<Type>&);
public:
// empty Queue
Queue():head(0),tail(0){}
// construct a Queue from a pair of iterators on some sequence
template <class It>
Queue(It beg,It end):
head(0),tail(0){ copy_elems(beg,end); }
// copy control to manage pointers to QueueItems in the Queue
Queue(const Queue &Q)
:head(0),tail(0){ copy_elems(Q); }
Queue& operator=(const Queue&); // left as exercise for the reader
~Queue() { destroy(); }
// replace current Queue by contents delimited by a pair of iterators
template <class Iter> void assign(Iter,Iter);
// return element from head of Queue
// unchecked operation:front on an empty Queue is undefined
Type& front() {return head->item; }
const Type &front() const {return head->item;}
void push(const Type &);
void pop();
bool empty()const{ //true if no elements in the Queue
return head == 0;
}
private:
QueueItem<Type> *head;
QueueItem<Type> *tail;
void destroy();
void copy_elems(const Queue&);
// version of copyto be used by assign to copy elements from iterator range
template <class Iter> void copy_elems(Iter,Iter);
}; // push 函数
// push 成员将新项放在队列末尾 template <class Type> void Queue<Type>::push(const Type &val)
{
// allocate a new QueueItem object
QueueItem<Type> *pt = new QueueItem<Type>(val);
// put item onto existing(眼下) queue
if(empty())
head = tail = pt; // the queue now has only one element
else {
tail->next = pt; // add new element to end of the queue
tail = pt;
}
} //copy_element 函数
template <class Type>
void Queue<Type>::copy_elems(const Queue &orig)
{
// copy elements from orig into this Queue
// loop stops when to pt == 0, which happens when we reach orig.tail
for (QueueItem<Type> *pt = orig.head; pt; pt = pt->next)
push(pt->item); // copy element
} // destroy 函数
template <class Type> void Queue<Type>::destroy()
{
while (!empty())
pop();
} //pop函数
template <class Type> void Queue<Type>::pop()
{
// pop is unchecked: Popping off an empty Queue is undefined
QueueItem<Type>* p = head;
head = head->next;
delete p;
} int main()
{
int n,val;
Queue<int> IntQ;
/*Queue<double> DouQ;
Queue<string> StrQ;*/
printf("Please enter you want total number: ");
std::cin>>n;
while(n){
std::cin>>val;
IntQ.push(val);
n--;
}
while(!IntQ.empty()){
std::cout<<IntQ.front()<<std::endl;
IntQ.pop();
}
return 0;
}

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. 3.Jmeter 快速入门教程(三-1) --添加响应断言(即loadrunner中所指的检查点)

    上一节课,我们创建了一个测试场景,并进行了少量vuser的负载测试. 有时候我们执行了测试,但是发现并不是所有事务都执行成功了. 那是因为我们只是发起了测试,但并没有对每次请求测试的返回作校验. 所以 ...

  2. python-前端JS

    JavaScript JS(JavaScript)和 JAVA是没有任何关系的,使前端语言动作的基础 特点:现在对前端所有的动作操作都是基于JS操作的.是一门逻辑语言 简单易用:可以使用任何文本编辑工 ...

  3. 轻松理解https,So easy!

    Java技术栈 www.javastack.cn 优秀的Java技术公众号 作者:翟志军 https://showme.codes/2017-02-20/understand-https/ 本文尝试一 ...

  4. 初探remoting双向通信(一)

    原 初探remoting双向通信(一) 2013年06月24日 15:47:07 喜欢特别冷的冬天下着雪 阅读数 4389 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blo ...

  5. Django rest_framework 频率控制组件

    频率控制 一.频率控制实现一 from rest_framework.views import APIView from rest_framework.response import Response ...

  6. MySQL 复制参数详解

    log-bin 二进制日志 server-id 早起版本必须添加  1-pow(2,32)-1 推荐使用 端口号+ip最后一位  5.6后可以动态修改 server-uuid (5.6以后) 默认存在 ...

  7. dubbo-源码阅读之Filter实现原理

    最近完成一个需求,使用阿里Arms需要在log里面加上traceId,但是发现dubbo异常 被ExceptionFilter捕获 并打印  打印不出traceI,然后百度搜索如何重写Filter 参 ...

  8. 将中国标准时间)转化为yyyy-MM-dd

    有两种方法: 1. ]); ) + '-' + d.getDate() + ' ' + d.getHours() + ':' + d.getMinutes() + ':' + d.getSeconds ...

  9. js 单行间隙滚动

    代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3. ...

  10. 项目实战 - 混合式App开发

    为何要使用混合式开发? 要说为什么使用Hybrid App [混合式开发],就要先了解什么是Native App[原生程序], Web App[网站程序]. Native App 是专门针对某一类移动 ...