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(迭代器),容器 ...
随机推荐
- 数据库(一)—— MySQL介绍
目录 MySQL介绍 一.MySQL版本 1.mysql主流版本 2.版本选择 二.MySQL连接与实例 1.MySQL的C/S结构 2.MySQL实例 三.mysql三层结构 1.连接层(连接上数据 ...
- python 模拟双色球输出
编写Python函数:完成一个双色球彩票的模拟生成过程, 其中前六个为红色球,数字范围1-33,不可重复.最后一个为蓝色球 1-16. import random #red_nums是采集红色球的数字 ...
- 转 关于Raid0,Raid1,Raid5,Raid10的总结
关于Raid0,Raid1,Raid5,Raid10的总结 RAID0 定义: RAID 0又称为Stripe或Striping,它代表了所有RAID级别中最高的存储性能.RAID 0提高存储性能 ...
- C语言函数指针用法
#include <stdio.h> #include <string.h> static void sayHello(); static void salute(); voi ...
- 【题解】sweet
题目描述 为了防止糖果被小猫偷吃,John把他的糖果放在了很多的高台上,一个高台可以认为是一段平行于X轴的线段,并且高台的y坐标都是大于0的,每个高台都有左端点和高台的长度,每个高台都有糖果.所有的高 ...
- Neo4j基础入门
图数据库基础知识 图数据库以图这种数据结构为基础,可以保存任意种类的数据,以下图为基础,简单介绍Neo4j中的几个简单概念: 1.节点(Nodes) 表示图数据库的实体(entities),代表图数据 ...
- linux终端命令行缩短显示路径
1,修改.bashrc文件(用户根目录下) vim 打开.bashrc文件,找到如下这行: else PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ ...
- python图像特征提取
这里使用的是python 3.5 .opencv_python-3.4.0+contrib,特征提取的代码如下: import cv2 img = cv2.imread("feature.j ...
- bzoj [POI2015]Myjnie
[POI2015]Myjnie Time Limit: 40 Sec Memory Limit: 256 MBSec Special Judge Description 有n家洗车店从左往右排成一排, ...
- python爬虫学习(3):使用User-Agent和代理ip
使用User-Agent方法一,先建立head,作为参数传进去 import urllib.requestimport json content=input("请输入需要翻译的内容:&quo ...