c++ simple class template example: Stack
main.cpp
#include "Stack.h" #include <iostream> using namespace std; class Box {
public:
Box():data(), ID(num++) { cout << "Box" << ID << " cons" << endl; }
// Notice that copy constructor and operator= must be implemented in a pairwise way.
// Because if you need Copy constructor, you almost definitely need to implement operator=
Box(const Box ©): data(copy.data), ID(num++) { cout << "Box" << ID << " copy cons" << endl; }
Box& operator=(const Box &b)
{
this->data = b.data;
return *this;
}
~Box() { cout << "Box" << ID << " des" << endl; }
int data;
private:
static int num;
const int ID;
}; int Box::num = ; int main()
{
Box b1,b2,b3;
b1.data = ;
b2.data = ;
b3.data = ;
Stack<Box> bstack;
bstack.push(b1);
bstack.push(b2);
bstack.push(b3);
while (!bstack.empty()) {
cout << bstack.top().data << endl;
bstack.pop();
}
return ;
}
Stack.h // Why there's no cpp file for Stack to hide implementation? See: http://www.cnblogs.com/qrlozte/p/4108807.html
#ifndef STACK_H
#define STACK_H #include <stdexcept> template <typename T>
class Stack
{
public:
Stack();
~Stack();
/**
Inserts a new element at the top of the stack,
above its current top element.
The content of this new element is
initialized to a copy of val.
@param val value to which the inserted element is initialized
*/
void push(const T &val);
/**
@return a reference to the top element in the stack
*/
T& top();
/**
@return a const reference to the top element in the stack
*/
const T& top() const;
/**
Removes the element on top of the stack.
This calls the removed element's destructor.
*/
void pop();
/**
@return the number of elements in the stack.
*/
size_t size() const; bool empty() const; private: template <typename TYPE>
class Link {
public: TYPE data;
Link *next; Link(const TYPE &_data, Link *_next = NULL): data(_data), next(_next) {}
~Link() {
next = NULL;
} }; Link<T> *head; size_t sz; // size, to avoid name conflict with Stack::size() }; template <typename T>
Stack<T>::Stack(): head(NULL), sz() {} template <typename T>
Stack<T>::~Stack() {
Link<T> *ptr = head;
while (ptr != NULL) {
ptr = head->next;
delete head;
head = ptr;
}
sz = ;
} /**
Inserts a new element at the top of the stack,
above its current top element.
The content of this new element is
initialized to a copy of val.
@param val value to which the inserted element is initialized
*/
template <typename T>
void Stack<T>::push(const T &val)
{
head = new Link<T>(val, head);
++sz;
}
/**
@return a reference to the top element in the stack
*/
template <typename T>
T& Stack<T>::top()
{
if (head == NULL)
throw std::runtime_error("empty stack");
return head->data; }
/**
@return a const reference to the top element in the stack
*/
template <typename T>
const T& Stack<T>::top() const
{
if (head == NULL)
throw std::runtime_error("empty stack");
return head->data;
}
/**
Removes the element on top of the stack.
This calls the removed element's destructor.
*/
template <typename T>
void Stack<T>::pop()
{
if (head == NULL)
throw std::runtime_error("empty stack");
Link<T> *ptr = head->next;
delete head;
head = ptr;
--sz;
} /**
@return the number of elements in the stack.
*/
template <typename T>
size_t Stack<T>::size() const {
return sz;
} template <typename T>
bool Stack<T>::empty() const
{
return (sz == );
} #endif // STACK_H
c++ simple class template example: Stack的更多相关文章
- A Simple C++ Template Class that Matches a String to a Wildcard Pattern
A recently implemented enhanced wildcard string matcher, features of which including, Supporting wil ...
- 堆栈 & Stack and Heap
What's the difference between a stack and a heap? The differences between the stack and the heap can ...
- C++ Template之类模版
类模版的定义和声明都和函数模版类似: 代码如下: template <typename T> class Stack { public: void push(const T&); ...
- javadataAbout stack and heap in JAVA(2)
改章节个人在上海喝咖啡的时候突然想到的...近期就有想写几篇关于javadata的笔记,所以回家到之后就奋笔疾书的写出来发表了 The stack is much faster than the he ...
- [转]C++ template —— 模板基础(一)
<C++ Template>对Template各个方面进行了较为深度详细的解析,故而本系列博客按书本的各章顺序编排,并只作为简单的读书笔记,详细讲解请购买原版书籍(绝对物超所值).---- ...
- C++ template —— 模板基础(一)
<C++ Template>对Template各个方面进行了较为深度详细的解析,故而本系列博客按书本的各章顺序编排,并只作为简单的读书笔记,详细讲解请购买原版书籍(绝对物超所值).---- ...
- 容器适配器(stack、 queue 、priority_queue)源码浅析与使用示例
一.容器适配器 stack queue priority_queue stack.queue.priority_queue 都不支持任一种迭代器,它们都是容器适配器类型,stack是用vector/d ...
- XML Publiser For Excel Template
1.XML Publisher定义数据 2.XML Publisher定义模板 模板类型选择Microsoft Excel,默认输出类型选择Excel,上传.xls模板 3.定义并发程序 4.定义请求 ...
- What’s the difference between a stack and a heap?
http://www.programmerinterview.com/index.php/data-structures/difference-between-stack-and-heap/ The ...
随机推荐
- Hadoop下大矩阵乘法Version2
1)使用本方法计算F*B,其中F是1000*1000的矩阵,B是1000*20000的矩阵,使用三个节点的集群,每个节点一个CPU核(集群装在虚拟机里,宿主机只有4个CPU核),每个节点配置一个map ...
- 个人python学习路线记录
一.入门视频 零基础入门学习Python --小甲鱼 二.博客园 python快速教程 http://www.cnblogs.com/vamei/archive/2012/09/13/2682778. ...
- EF需要注意的virtual,懒加载,还有1对n更新
1.如果实体类型有任何一个集合属性是 virtual 的,那么该属性会懒加载,在查询该对象时,看到的类型是代理对象(proxy_xxxx), 使用new来更新1对n关系时会 增加 ).FirstOrD ...
- 图解http读书笔记
以前对HTTP协议一知半解,一直不清楚前端需要对于HTTP了解到什么程度,知道接触的东西多了,对于性能优化.服务端的配合和学习中也渐渐了解到了HTTP基础的重要性,看了一些大神对HTTP书籍的推荐,也 ...
- Swift,枚举
枚举类型判断 1.设置并利用枚举 enum Weacher{ case a case b case c } var d=Weacher.b switch d{ case .a: print(" ...
- [web] Get和Post区别,EncType提交数据的格式详解
转载自:http://www.cnblogs.com/sunxucool/archive/2012/12/11/2813113.html 1. get是从服务器上获取数据,post是向服务器传送数据. ...
- python 列表合并
列表合并主要有以下方法: 1.用list的extend方法,L1.extend(L2),该方法将参数L2的全部元素添加到L1的尾部 结果:[1, 2, 3, 4, 5, 1, 20, 30] 2.用切 ...
- 验收测试 - WebDriver 5
验收测试 - WebDriver - 配置 什么是WebDriver 这样说好了,它翻译起来就是Web驱动,用我的经验来说,它就是驱动浏览器运行的一个驱动器 有什么作用? 就像一个司机可以驱动一台汽车 ...
- Python连接MySQL乱码(中文变问号)
#coding=utf-8 import MySQLdb db = MySQLdb.connect("IP","用户名","密码",&quo ...
- mui.fire() 和 mui.trigger()
导读:添加自定义事件监听操作和标准js事件监听类似,可直接通过window对象添加,通过mui.fire()方法可触发目标窗口的自定义事件 监听自定义事件 添加自定义事件监听操作和标准js事件监听类似 ...