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 &copy): 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的更多相关文章

  1. 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 ...

  2. 堆栈 & Stack and Heap

    What's the difference between a stack and a heap? The differences between the stack and the heap can ...

  3. C++ Template之类模版

    类模版的定义和声明都和函数模版类似: 代码如下: template <typename T> class Stack { public: void push(const T&); ...

  4. javadataAbout stack and heap in JAVA(2)

    改章节个人在上海喝咖啡的时候突然想到的...近期就有想写几篇关于javadata的笔记,所以回家到之后就奋笔疾书的写出来发表了 The stack is much faster than the he ...

  5. [转]C++ template —— 模板基础(一)

    <C++ Template>对Template各个方面进行了较为深度详细的解析,故而本系列博客按书本的各章顺序编排,并只作为简单的读书笔记,详细讲解请购买原版书籍(绝对物超所值).---- ...

  6. C++ template —— 模板基础(一)

    <C++ Template>对Template各个方面进行了较为深度详细的解析,故而本系列博客按书本的各章顺序编排,并只作为简单的读书笔记,详细讲解请购买原版书籍(绝对物超所值).---- ...

  7. 容器适配器(stack、 queue 、priority_queue)源码浅析与使用示例

    一.容器适配器 stack queue priority_queue stack.queue.priority_queue 都不支持任一种迭代器,它们都是容器适配器类型,stack是用vector/d ...

  8. XML Publiser For Excel Template

    1.XML Publisher定义数据 2.XML Publisher定义模板 模板类型选择Microsoft Excel,默认输出类型选择Excel,上传.xls模板 3.定义并发程序 4.定义请求 ...

  9. What’s the difference between a stack and a heap?

    http://www.programmerinterview.com/index.php/data-structures/difference-between-stack-and-heap/ The ...

随机推荐

  1. 【费用流】bzoj1520 [POI2006]Szk-Schools

    注意:建图的时候,一定要把旧标号相同的分开. #include<cstdio> #include<algorithm> #include<cstring> #inc ...

  2. 输入格式CombineFileInput

    此输入格式的作用就是可以将来自多个不同文件的物理块作为一个split,然后由一个map进行处理. http://www.blogjava.net/shenh062326/archive/2012/07 ...

  3. 13test02:阶乘

    //假设32位int型变量y是表示最大人数的x的阶乘,即y=x!,当x最大值取什么时,y取最大值 //,且乘法不溢出. #include<iostream> using namespace ...

  4. ionic 打包成apk后,所有网络请求404

    无论怎么改 config.xml <allow-navigation href="http://*/*" /> <allow-intent href=" ...

  5. [HTML/CSS]盒子模型,块级元素和行内元素

    目录 概述 盒子模型 块级元素 行内元素 可变元素 总结 概述 在div+css中,了解块级元素和行内元素还是非常有必要的,比如:对行内元素使用width属性就会失效.虽然自己不是做前端的,但是,在项 ...

  6. CSS3:固定textarea文本域宽度

    textarea在一些浏览器上可以被拖拉改变大小,为了保持美观,可以通过 CSS3 resize 属性禁掉 textarea{resize:none; //不允许用户调整元素大小}

  7. Web服务器在外网能裸奔多久?

      很多时候我们轻易地把Web服务器暴露在公网上,查看一下访问日志,可以看到会收到大量的攻击请求,这个是网站开通后几个小时收到的请求: 1.  探测服务器信息 在上线一分钟,收到OPTION请求探测. ...

  8. ylbtech-LanguageSamples-CollectionClasses(集合类)

    ylbtech-Microsoft-CSharpSamples:ylbtech-LanguageSamples-CollectionClasses(集合类) 1.A,示例(Sample) 返回顶部 “ ...

  9. 深度增强学习--Policy Gradient

    前面都是value based的方法,现在看一种直接预测动作的方法 Policy Based Policy Gradient 一个介绍 karpathy的博客 一个推导 下面的例子实现的REINFOR ...

  10. 实现一个Cglib代理Demo

    Cglib动态代理采用的是创建目标类的子类的方式.优点:不用实现额外接口,只操作我们关心类,高性能. package jesse.test; import java.lang.reflect.Meth ...