C++模板中的嵌套
在下面的程序中,我们创建了一个模板类用于实现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++模板中的嵌套的更多相关文章
- Thinkphp 模板中 if 嵌套层级过多的问题,嵌套3级就报错,取消层级限制
解决此问题有两种办法:1.第三层if换成eq或者原生<?php 'abc';> 2.修改Tp核心配置文件 1.第三层if换成eq或者原生<?php 'abc';> 如下图&l ...
- ThinkPHP问题收集:模板中使用U方法时无法嵌套大括号,For标签,插入数据,新增的表字段缓存问题
ThinkPHP模板中使用U方法时无法嵌套大括号需要在control里面用U方法赋值给变量传到模版如:{:U('/Blog/comment/',array('id'=>$id)}$comment ...
- 测试开发之Django——No6.Django模板中的标签语言
模板中的标签语言 1.if/else {% if %} 标签检查(evaluate)一个变量,如果这个变量为真(即:变量存在,非空,不是布尔值假),系统会显示在{% if %} 和 {% endi ...
- <转>详解C++的模板中typename关键字的用法
用处1, 用在模板定义里, 标明其后的模板参数是类型参数. 例如: template<typename T, typename Y> T foo(const T& t, const ...
- flask的jinja2模板中过过滤器的相关小内容
jinja2模板中有自带的过滤器,有需要直接拿来使用.也可以自己定义过滤器 在过滤器中,有一些常见得操作及关键字.有对字符串的操作,还有对大小写转换的操作.还有对list的操作 过滤器的语法 {# 过 ...
- JavaScript获取Django模板中指定键值的数据,使用过滤器
Django中利用js来操作数据的常规操作一般为点(.)操作符来获取字典或列表的数据,一般如{{data.0}},{{data.arg}} 但有时如果数据是嵌套类型的数据时,直接获取某个值就变得困难了 ...
- ThinkPHP+Smarty模板中截取包含中英文混合的字符串乱码的解决方案
好几天没写博客了,其实有好多需要总结的,因为最近一直在忙着做项目,但是困惑了几天的Smarty模板中截取包含中英文混合的字符串乱码的问题,终于解决了,所以记录下来,需要的朋友看一下: 出现乱码的原因: ...
- SMARTY模板中如何使用get,post,request,cookies,session,server变量
{$smarty}保留变量不需要从PHP脚本中分配,是可以在模板中直接访问的数组类型变量,通常被用于访问一些特殊的模板变量.例如,直接在模板中访问页面请求变量.获取访问模板时的时间戳.直接访问PHP中 ...
- django url路径与模板中样式相对路径的问题
static目录下有css和js及image等文件夹,里面放置网站的一些静态文件,static位于网站根目录下,django中配置静态文件这个就细说,网上都有,昨天在添加新内容时发现一个问题,我的ur ...
随机推荐
- iOS开关按钮UISwitch控件
开关按钮UISwitch 在ViewController.h里面 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #import <UIKit/UIKit ...
- Java web 项目读取src或者tomcat下class文件夹下的xml文件或者properties文件
//生成一个文件对象: File file = new File(getClass().getClassLoader().getResource("test.xml").getPa ...
- Apache HttpComponents POST提交带参数提交
public class Test { public static void main(String[] args) throws IOException { DefaultHttpClient ht ...
- win10设置删除文件提示框
显示桌面,找到回收站 点击鼠标右键,点击“属性菜单” 勾选“显示删除对话框” 点击“应用”,点击“确定”. 测试一下吧,从电脑删除del删除一个文件.如下图所示,弹出了提示框.
- C++实现顺序计算输入表达式的值
#include <iostream> #include <cstring> #include <cctype>//判断字符类型需要的头文件 using names ...
- JavaScript 关键字快速匹配
来源: http://www.cnblogs.com/index-html/archive/2013/04/17/js_keyword_match.html http://www.etherdream ...
- MySQL 常用语法 之 DISTINCT
DISTINCT作用很简单就是去除重复行的数据. 具体看下面列子 表A数据[两条 nami 99] nameA scoreA robin 98 nami 99 saber 98 lu ...
- mips cfe命令
设置串口参数 setenv -p LINUX_CMDLINE "console=ttyS0,115200 root=mtd4 rw rootfstype=jffs2" 内核启动参数 ...
- UART通信协议
第一部分: UART使用的是 异步,串行通信. 串行通信是指利用一条传输线将资料一位位地顺序传送.特点是通信线路简单,利用简单的线缆就可实现通信,降低成本,适用于远距离通信,但传输速度慢的应用场 ...
- R语言绘图边框
在R语言中, 绘图边框一共有3个区域: device region : figure region : plot region : 在描述不同区域大小的时候,有对应的不同参数: din : 返回d ...