在下面的程序中,我们创建了一个模板类用于实现Queue容器的部分功能,并且在模板类中潜逃使用了一个Node类。
queuetp.h

// queuetp.h -- queue template with a nested class
#ifndef QUEUETP_H_
#define QUEUETP_H_ template <class Item>
class QueueTP
{
private:
enum {Q_SIZE = };
// Node is a nested class definition
class Node
{
public:
Item item;
Node * next;
Node(const Item & i) : item(i), next() {}
};
Node * front; // pointer to front of Queue
Node * rear; // pointer to rear of Queue
int items; // current number of items in Queue
const int qsize; // maximum number of items in Queue
QueueTP(const QueueTP & q) : qsize() {}
QueueTP & operator=(const QueueTP & q) { return *this; }
public:
QueueTP(int qs = Q_SIZE);
~QueueTP();
bool isempty() const
{
return items == ;
}
bool isfull() const
{
return items == qsize;
}
int queuecount() const
{
return items;
}
bool enqueue(const Item &item); // add item to end
bool dequeue(Item &item); // remove item from front
};
// QueueTP methods
template <class Item>
QueueTP<Item>::QueueTP(int qs) : qsize(qs)
{
front = rear = ;
items = ;
} template <class Item>
QueueTP<Item>::~QueueTP()
{
Node * temp;
while (front != ) // while queue is not yet empty
{
temp = front;
front = front->next;
delete temp;
}
} // Add item to queue
template <class Item>
bool QueueTP<Item>::enqueue(const Item & item)
{
if (isfull())
return false;
Node * add = new Node(item); // create node
// on failure, new throws std::bad_alloc exception
items ++;
if (front == ) // if queue is empty
front = add; // place item at front
else
rear->next = add; // else place at rear
rear = add;
return true;
} // Place front item into item variable and remove from queue
template <class Item>
bool QueueTP<Item>::dequeue(Item & item)
{
if (front == )
return false;
item = front->item; // set item to first item in queue
items --;
Node * temp = front; // save location of first item
front = front->next; // reset front to next item
delete temp; // delete former first item
if (items == )
rear = ;
return true;
} #endif // QUEUETP_H_

这里,Node是利用通用类型Item类定义的。所以,下面的声明将导致Node被定义成用于存储double值:
QueueTp<double> dq;
而下面的声明将导致Node被定义沉用于存储char值:
QueueTp<char> cq;
这两个Node类将在两个独立的QueueTP类中定义,因此不会发生名称冲突。即一个节点的类型为QueueTP<double>::Node,另一个节点的类型为QueueTP<char>::Node。
下面的程序用于测试这个新的类。
nested.cpp

//nested.cpp -- using a queue that has a nested class
#include <iostream> #include <string>
#include "queuetp.h" int main()
{
using std::string;
using std::cin;
using std::cout; QueueTP<string> cs();
string temp; while (!cs.isfull())
{
cout << "Please enter your name. You will be "
<< "served in the order of arrival.\n"
<< "name: ";
getline(cin, temp);
cs.enqueue(temp);
}
cout << "The queue is full. Processing begins!\n"; while (!cs.isempty())
{
cs.dequeue(temp);
cout << "Now processing " << temp << "...\n";
}
return ;
}

效果:

Please enter your name. You will be served in the order of arrival.
name: moonlit
Please enter your name. You will be served in the order of arrival.
name: moon
Please enter your name. You will be served in the order of arrival.
name: light
Please enter your name. You will be served in the order of arrival.
name: moonlight poet
Please enter your name. You will be served in the order of arrival.
name: paul jackson
The queue is full. Processing begins!
Now processing moonlit...
Now processing moon...
Now processing light...
Now processing moonlight poet...
Now processing paul jackson...

C++模板中的嵌套的更多相关文章

  1. Thinkphp 模板中 if 嵌套层级过多的问题,嵌套3级就报错,取消层级限制

    解决此问题有两种办法:1.第三层if换成eq或者原生<?php 'abc';>  2.修改Tp核心配置文件 1.第三层if换成eq或者原生<?php 'abc';> 如下图&l ...

  2. ThinkPHP问题收集:模板中使用U方法时无法嵌套大括号,For标签,插入数据,新增的表字段缓存问题

    ThinkPHP模板中使用U方法时无法嵌套大括号需要在control里面用U方法赋值给变量传到模版如:{:U('/Blog/comment/',array('id'=>$id)}$comment ...

  3. 测试开发之Django——No6.Django模板中的标签语言

    模板中的标签语言 1.if/else {% if  %} 标签检查(evaluate)一个变量,如果这个变量为真(即:变量存在,非空,不是布尔值假),系统会显示在{% if  %} 和 {% endi ...

  4. <转>详解C++的模板中typename关键字的用法

    用处1, 用在模板定义里, 标明其后的模板参数是类型参数. 例如: template<typename T, typename Y> T foo(const T& t, const ...

  5. flask的jinja2模板中过过滤器的相关小内容

    jinja2模板中有自带的过滤器,有需要直接拿来使用.也可以自己定义过滤器 在过滤器中,有一些常见得操作及关键字.有对字符串的操作,还有对大小写转换的操作.还有对list的操作 过滤器的语法 {# 过 ...

  6. JavaScript获取Django模板中指定键值的数据,使用过滤器

    Django中利用js来操作数据的常规操作一般为点(.)操作符来获取字典或列表的数据,一般如{{data.0}},{{data.arg}} 但有时如果数据是嵌套类型的数据时,直接获取某个值就变得困难了 ...

  7. ThinkPHP+Smarty模板中截取包含中英文混合的字符串乱码的解决方案

    好几天没写博客了,其实有好多需要总结的,因为最近一直在忙着做项目,但是困惑了几天的Smarty模板中截取包含中英文混合的字符串乱码的问题,终于解决了,所以记录下来,需要的朋友看一下: 出现乱码的原因: ...

  8. SMARTY模板中如何使用get,post,request,cookies,session,server变量

    {$smarty}保留变量不需要从PHP脚本中分配,是可以在模板中直接访问的数组类型变量,通常被用于访问一些特殊的模板变量.例如,直接在模板中访问页面请求变量.获取访问模板时的时间戳.直接访问PHP中 ...

  9. django url路径与模板中样式相对路径的问题

    static目录下有css和js及image等文件夹,里面放置网站的一些静态文件,static位于网站根目录下,django中配置静态文件这个就细说,网上都有,昨天在添加新内容时发现一个问题,我的ur ...

随机推荐

  1. python 调用函数 / 类型转换 / 切片/ 迭代

    调用函数 / 类型转换 /  切片/ 迭代 1. 调用函数:abs(),max(),min() 2. 数据类型转换:int(),float(),str(),tool(),a=abs, 3. 定义函数, ...

  2. 【Unity】用代码给按钮动态添加点击事件

    问题:多数情况下用UGUI的Button控件身上的OnClick()列表可以指明该按钮点击后触发的回调.现在想要调用自定义脚本里的方法,当这个脚本挂在Button所属的Canvas身上时,传入Canv ...

  3. C语言 · 第二大整数

    算法提高 第二大整数   时间限制:1.0s   内存限制:512.0MB      问题描述 编写一个程序,读入一组整数(不超过20个),当用户输入0时,表示输入结束.然后程序将从这组整数中,把第二 ...

  4. 千兆网口POE供电

    一.IEEE802.3af与at标准的解析 链接:http://www.winchen.com.cn/ShowNews2.asp?ID=21&ClassID=1 2003 年6 月,IEEE  ...

  5. C++ 递归实现汉诺塔

    C++实现汉诺塔 #include <iostream> using namespace std; void move(int n,char x,char y,char z) { ) { ...

  6. firewalled centos7

    zone绑定网卡 firewall-cmd --zone=internal --add-interface=ens192 --permanent firewall-cmd --permanent -- ...

  7. windows rails new demo时候出错Make sure that `gem install mysql2 -v '0.3.15'` succeeds before bundling.

    rails new demo --database=mysql最后报错Gem files will remain installed in D:/BillFiles/rails_dev/Ruby193 ...

  8. main函数位置

    c语言中main函数的位置可以任意位置.在执行一个c语言编写的程序时,main函数就相当于是执行程序的入口.只要是没有语法和逻辑上的错误,main函数可以放在任意位置.

  9. python with妙用

    class aa(): def bb(self): print("hhhh") return "hello world" def __enter__(self) ...

  10. imx6 uart分析

    本文主要记录: 1.uart设备注册 2.uart驱动注册 3.上层应用调用有些地方理解的还不是很透彻,希望指正. 1.uart设备注册过程 MACHINE_START(MX6Q_SABRESD, & ...