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. 【二分图判定】hdu3478 Catch

    详细的题解:http://www.docin.com/p-517243379.html 一个图是二分图 等价于 其至少有两个节点且没有奇环. 二分图判定的方法:从任意点出发进行一次dfs黑白染色,若某 ...

  2. centos7下解决python3和python2同时存在但是无法使用pip3的问题

    一.首先,官网下载python3的所需版本. wget https://www.python.org/ftp/python/3.6.0/Python-3.6.0.tgz 想下载到那个文件夹下就先进入到 ...

  3. lua的luasocket程序

    -- load namespace local socket = require("socket") -- create a TCP socket and bind it to t ...

  4. 找出分数最高的前两个学生 Exercise05_09

    import java.util.Scanner; /** * @author 冰樱梦 * 时间:2018年下半年 * 题目:找出分数最高的前两个学生 * */ public class Exerci ...

  5. Exercise02_17

    import javax.swing.JOptionPane; public class FrostTemperature { public static void main(String[] arg ...

  6. Exercise01_07

    public class Outcome{ public static void main(String[] args){ double x, y; x=4*(1.0-1.0/3+1.0/5-1.0/ ...

  7. Centos:mysql的安装和使用:yum方式

    1.安装: 安装客户端 sudo yum install mysql 安装服务器 sudo yum install mysql-server 2.配置:查看配置文件 cat /etc/my.cnf 3 ...

  8. scrapy-splash抓取动态数据例子九

    一.介绍 本例子用scrapy-splash抓取众视网网站给定关键字抓取咨询信息. 给定关键字:个性化:融合:电视 抓取信息内如下: 1.资讯标题 2.资讯链接 3.资讯时间 4.资讯来源 二.网站信 ...

  9. 有哪些通俗易懂的例子可以解释 IaaS、PaaS、SaaS 的区别?

    有哪些通俗易懂的例子可以解释 IaaS.PaaS.SaaS 的区别? S 软件 P 中间件 I 基础设施

  10. 模板:什么是Traits

    Traits不是一种语法特性,而是一种模板编程技巧.Traits在C++标准库,尤其是STL中,有着不可替代的作用.   如何在编译期间区分类型   下面我们看一个实例,有四个类,Farm.Worke ...