STL 队列模板实现
版权声明:本文为博主原创文章。未经博主同意不得转载。 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 队列模板实现的更多相关文章
- C++ 标准模板库STL 队列 queue 使用方法与应用介绍
C++ 标准模板库STL 队列 queue 使用方法与应用介绍 queue queue模板类的定义在<queue>头文件中. 与stack模板类很相似,queue模板类也需要两个模板参数, ...
- 模板——STL队列
C++ STL queue 容器优先队列&&队列 队列 #include<queue> #include<iostream> using namespace s ...
- STL标准模板库介绍
1. STL介绍 标准模板库STL是当今每个从事C++编程的人需要掌握的技术,所有很有必要总结下 本文将介绍STL并探讨它的三个主要概念:容器.迭代器.算法. STL的最大特点就是: 数据结构和算法的 ...
- luoguP1886 滑动窗口(单调队列模板题)
题目链接:https://www.luogu.org/problem/P1886#submit 题意:给定n个数,求大小为k的滑动窗口中最小值和最大值. 思路:单调队列模板题. AC代码: #incl ...
- Sliding Window POJ - 2823 单调队列模板题
Sliding Window POJ - 2823 单调队列模板题 题意 给出一个数列 并且给出一个数m 问每个连续的m中的最小\最大值是多少,并输出 思路 使用单调队列来写,拿最小值来举例 要求区间 ...
- STL学习系列一:STL(标准模板库)理论基础
STL(Standard Template Library,标准模板库)是惠普实验室开发的一系列软件的统称.现然主要出现在C++中,但在被引入C++之前该技术就已经存在了很长的一段时间. STL的从广 ...
- STL(标准模板库)理论基础,容器,迭代器,算法
基本概念 STL(Standard Template Library,标准模板库)是惠普实验室开发的一系列软件的统称.现然主要出现在C++中,但在被引入C++之前该技术就已经存在了很长的一段时间. ...
- STL标准模板类
STL,中文名标准模板库,是一套C++的标准模板类(是类!),包含一些模板类和函数,提供常用的算法和数据结构. STL分为:迭代器,容器,适配器,算法以及函数对象. --迭代器是一种检查容器内元素并遍 ...
- STL(标准模板库)基本概念
一.什么是STL STL(Standard Template Library,标准模板库)的从广义上讲分为三类:algorithm(算法).container(容器)和iterator(迭代器),容器 ...
随机推荐
- 基于MFC的Media Player播放器的制作(1---播放器界面的布局)
| 版权声明:本文为博主原创文章,未经博主允许不得转载. 通过上面的一些预备知识,我们现在就可以自己来制作基于MFC的播放器了,接下来我们讲的是使用MFC制作我们播放器 的界面. 首先,我们我们打 ...
- os.fork()----linux
fork() 函数,它也属于一个内建并 且只在 Linux 系统下存在. 它非常特殊普通的函数调用,一次返 回但是 fork() 调用一次,返回两次.因为操作系统自动把当前进程(称为父)复制了一份(称 ...
- mybatis之参数处理
mybatis的mapper接口需要和mapper映射文件进行关联,上一节已经展示接口方法的返回值是如何和mapper映射文件的resultType进行映射.这一节主要讲解接口方法的参数如何与映射文件 ...
- [BJWC2018]上学路线
Description 小B 所在的城市的道路构成了一个方形网格,它的西南角为(0,0),东北角为(N,M). 小B 家住在西南角,学校在东北角.现在有T 个路口进行施工,小B 不能通过这些路口.小B ...
- android中的文件存储
Context类中提供的IO方法: FileOutputSream openFileOutput(String name, int mode):打开应用程序中的/data/data/<应用程序的 ...
- spring_AOP的注解开发
logger日志类: package cn.mepu.utils; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.la ...
- Fiddler手机抓包配置
之前按照网上教程配置,发现还是不太详细,做下笔记备忘 一.电脑端配置 因为手机需要配置电脑IP,如果我们IP自动获取,某一时刻IP可能会被改变,此时手机端无法看到抓包信息, 所以第一步,需要确认电脑端 ...
- win10配置jdk环境变量及遇到的坑
第一步.在系统变量中新增变量命名JAVA_HOME,值为jdk的安装目录 JAVA_HOME C:\Program Files\Java\jdk1.8.0_231 第二步.在系统变量中增加path的值 ...
- 通过实例详细讲解PHP垃圾回收机制
PHP垃圾回收机制:1. PHP可以自动进行内存管理,清除不需要的对象,主要使用了引用计数2. 在zval结构体中定义了ref_count和is_ref , ref_count是引用计数 ,标识此zv ...
- 浅谈无线h5开发
最近一直在做h5的项目,对h5开发有了自己的理解.首先h5开发并不是指的html5的开发,而是指无线端的web开发,至于为什么叫h5开发,我觉得一方面是因为html5近几年还是挺受关注,另一方面h5在 ...