c++ 模板学习笔记:用数组和类模板模拟通用栈(权哥)
2013-11-24 12:16
483 查看
初学c++模板类,此处拾人牙慧,用数组和类模板模拟通用栈
#include <iostream> #include <typeinfo> #include <string> #include <cstring> #include <exception> using namespace std; template <typename T=int,int len=10> class stack { T a[len]; int cur; public: stack():cur(0){} const char* element(){return typeid(T).name();} int max_size(){return len;} bool empty(){return cur==0;} bool full(){return cur==len;} int size(){return cur;} void push(const T& d)throw(const char*){ if(full()) throw("full"); a[cur++]=d; } T pop() throw(const char*){ if(empty()) throw("empty"); return a[--cur]; } const T& top(){return a[cur-1];}; void clear(){cur=0;} }; template <int len> class stack<const char*,len> { string a[len]; int cur; public: stack():cur(0){} const char* element(){return "const char*";} int max_size(){return len;} bool empty(){return cur==0;} bool full(){return cur==len;} int size(){return cur;} void push(const char *d)throw(const char*){ if(full()) throw("full"); a[cur++]=d; } const char* pop() throw(const char*){ if(empty()) throw("empty"); return a[--cur].c_str(); } /*如果返回const char*& 会编译出错: error: invalid initialization of non-const reference of type ‘const char*&’ from a temporary of type ‘const char*’. 如果返回const char* const& 编译不报错,会有warning: returning reference to temporary*/ const char* const& top() throw(const char*){ if(empty()) throw("empty"); return a[cur-1].c_str(); } void clear(){cur=0;} }; int main() { stack<int> si; si.push(1); si.push(2); si.push(3); si.push(4); cout << "top:" << si.top() << endl; cout << "element type:" << si.element() << endl; cout << "max_size:" << si.max_size() << ',' << "size:" << si.size() << endl; while(!si.empty()) cout << si.pop() << ' '; cout << "\n===============================\n"; stack<char,15> sc; sc.push('+'); sc.push('-'); sc.push('*'); sc.push('/'); cout << "top:" << sc.top() << endl; cout << "element type:" << sc.element() << endl; cout << "max_size:" << sc.max_size() << ',' << "size:" << sc.size() << endl; while(!sc.empty()) cout << sc.pop() << ' '; cout << "\n===============================\n"; stack<const char*> scp; char buf[20]; while(1){ cin >> buf; if(!strcmp(buf,"end")) break; scp.push(buf); } cout << "element type:" << scp.element() << endl; cout << "max_size:" << scp.max_size() << ',' << "size:" << scp.size() << endl; cout << "top:" << scp.top() << endl; while(!scp.empty()) cout << scp.pop() << ' '; cout << "\n===============================\n"; return 0; }
相关文章推荐
- c++ 模板学习笔记:函数模板实现数组通用排序和遍历打印(权哥)
- c++ 模板学习笔记:类模板模拟auto_ptr智能指针(权哥)
- c++ 模板学习笔记:函数模板的类型识别(权哥)
- c++ 模板学习笔记:理解类模板的特化与偏特化(权哥)
- c++ 模板学习笔记:类模板和函数模板实现pair(权哥)
- 金庸小说考试之模板方法模式学习笔记[C++版]
- “模板”学习笔记(7)-----数组模板+对象数组举例
- 我的小小学习笔记(7)之C++------模板之模板函数
- C++学习笔记十六-模板和泛型编程(一)
- 【C++】学习笔记草稿版18(模板)
- C++学习笔记——C++动态数组的创建与删除
- C++学习笔记12-模板1
- c++学习笔记 -- 函数模板与类模板的综合运用
- 二.C++学习笔记-模板
- C++ Primer 学习笔记(第三章:字符串、向量和数组)
- 设计模式C++学习笔记之九(Template Method模板方法模式)
- C++学习笔记十六-模板和泛型编程(二)
- C++学习笔记四之使用new来创建动态数组
- C++学习笔记十六-模板和泛型编程(一)
- member template成员模板笔记----C++学习之路