在下面的程序中,我们创建了一个模板类用于实现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. css让footer永远保持在页面底部

    案例1:仅仅保存在页面底部.不固定. 思路: html: <div class="body"> <header>我是头部</header> &l ...

  2. Unity又称Unity Application Block

    本文关注以下方面(环境为VS2012..Net Framework 4.5以及Unity 3): Ioc/DI简介: Unity简单示例 一.Ioc/DI简介 IoC 即 Inversion of C ...

  3. Liunx 安装 Mysql 5.7

    #[安装 Mysql 5.7] # 00.系统目录说明# 安装文件下载目录:/data/software# Mysql目录安装位置:/usr/local/mysql# 数据库保存位置:/data/my ...

  4. Apache HttpComponents 获取页面内容String方式

    /* * ==================================================================== * Licensed to the Apache S ...

  5. 云端中间层负载均衡工具 Eureka

    亚马逊提供了一个负载均衡工具 Elastic Load Balancer,但针对的是终端用户 Web 流量服务器,而 Eureka 针对的是中间层服务器的负载均衡.AWS 固有的环境,对 IP 地址. ...

  6. C++实现 找出10000以内的完数

    C++实现 找出10000以内的完数 #include <stdio.h> int main(){ int n; // 用户输入的整数 int i; // 循环标志 printf(&quo ...

  7. java多线程面试题整理及回答

    1)现在有T1.T2.T3三个线程,你怎样保证T2在T1执行完后执行,T3在T2执行完后执行? 这个线程问题通常会在第一轮或电话面试阶段被问到,目的是检测你对”join”方法是否熟悉.这个多线程问题比 ...

  8. The Qt Resource System

    The Qt Resource System The Qt resource system is a platform-independent mechanism for storing binary ...

  9. win7配置ftp服务器

    1.安装FTP,打开控制面板,找到程序和功能-->打开或者关闭windows功能,在列表中,展开internet信息服务,勾选FTP服务器,展开WEB管理工具,勾选IIS管理控制台,然后点击确定 ...

  10. Jquery实用代码片段(转)

    1.把所有带有#的空链接换成不友情的链接 $('a[href="#"]').each(function() { $(this).attr('href', 'javascript:v ...