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 ...
随机推荐
- 【分块】bzoj3196 Tyvj 1730 二逼平衡树
分块 或 树套树. 在每个块中维护一个有序表,查询时各种二分,全都是分块的经典操作,就不详细说了. 块的大小定为sqrt(n*log2(n))比较快. #include<cstdio> # ...
- 【网络流】【Dinic】【Next Array】Dinic模板
注意:有时加边不一定要加反向弧. Next Array版. #include<cstdio> #include<cstring> #include<algorithm&g ...
- redis命令行清缓存
任务管理器-服务-redis 右击打开详细信息--右击打开文件位置 在这个位置cmd 输入命令redis-cli 在输入命令:flushall 出现ok即清除缓存成功
- cocoods 出现下面的问题:ERROR: While executing gem ... (Errno::EPERM)
今天安装cocoods 出现下面的问题:ERROR: While executing gem ... (Errno::EPERM) Operation not permitted - /us ...
- Swift,闭包
闭包(相当于匿名函数)的几种情况 利用sorted()排序方法来进行示例 1.sorted()正常引用的情况 var str=["d","a","c& ...
- python获取linux本机IP
#!/usr/bin/env python #encoding: utf-8 #description: get local ip address import os import socket, f ...
- solr6.6 配置自带中文分词
1.配置solrconfig.xml solr的自带中文分词包在solr-6.6.0\contrib\analysis-extras\lucene-libs下 修改solrconfig.xml增加 & ...
- Docker解析及轻量级PaaS平台演练(三)--Dockerfile编写
在本篇中将介绍Dockerfile的编写 除了通过修改Image,创建Container,在打包成Image来创建我们需要的Image之外 我们还可以编写Dockerfile文件,通过build来创建 ...
- 倍福TwinCAT(贝福Beckhoff)应用教程12.1 TwinCAT控制松下伺服 连接和试运行
首先是用松下伺服自带的软件可以测试运行(驱动器,电机都连接好,然后用USB线连接到松下伺服驱动器的X1口),打开调试软件会自动提示连接到伺服 一般需要对驱动器清除绝对值编码器数据(驱动器可能报错4 ...
- UIScreenAdaptive
using UnityEngine; namespace Com.Xyz.UI { [ExecuteInEditMode] [RequireComponent(typeof(UIRoot))] pub ...