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(迭代器),容器 ...
随机推荐
- Django框架(十六)—— cookie和session组件
目录 cookie和session组件 一.cookie 1.cookie的由来 2.什么是cookie 3.cookie的原理 4.cookie的覆盖 5.在浏览器中查看cookie 6.cooki ...
- 元类,sqlalchemy查询
import sqlalchemy from sqlalchemy.ext.declarative import declarative_base #创建连接实例 db = sqlalchemy.cr ...
- linux性能查看调优
一 linux服务器性能查看1.1 cpu性能查看1.查看物理cpu个数:cat /proc/cpuinfo |grep "physical id"|sort|uniq|wc -l ...
- 数据结构与算法简记--redis有序集合实现-跳跃表
跳表 定义 为一个值有序的链表建立多级索引,比如每2个节点提取一个节点到上一级,我们把抽出来的那一级叫做索引或索引层.如下图所示,其中down表示down指针,指向下一级节点.以此类推,对于节点数为n ...
- Centos7.6安装教程 && history设置显示执行命令的时间
一.规划磁盘使用空间(磁盘总大小200GB) /dev/sda1 mount /boot 1G mount point /dev/sda2 mount / 100G /dev/sda3 mount / ...
- 18-vim-插入命令
在vi中除了常用的i进入编辑模式外,还提供了以下命令同样可以进入编辑模式: 命令 英文 功能 使用频率 i insert 在当前字符前插入文本 常用 I insert 在行首插入文本 较常用 a ap ...
- UltraEdit常用快捷键
UltraEdit是一套功能强大的文本编辑器,可以编辑文本.十六进制.ASCII码,可以取代记事本,内建英文单字检查.C++及VB指令突显,可同时编辑多个文件,而且即使开启很大的文件速度也不会慢. 说 ...
- 自定义DbUtils通用类
本实例使用C3P0连接池做连接,详见https://www.cnblogs.com/qf123/p/10097662.html开源连接池C3P0的使用 DBUtils.java package com ...
- mysql的一些语法
alter table S61.T6198 modify F12 varchar(30) DEFAULT NULL COMMENT '流水号'; 修改表字段结构. mysql不支持top ,只支持l ...
- stdin stdout stderr - 标准 I/O 流
Fd #include <stdio.h> Fd extern FILE *stdin; Fd extern FILE *stdout; Fd extern FILE *stderr; D ...