C++ 类模板
来源:互联网 发布:广东省网络医院 后窖 编辑:程序博客网 时间:2024/06/03 18:32
《C++ Primer Plus》
C++中为什么用模板类?
- ( 1 ) 可用来创建动态增长和减小的数据结构
- (2)它是类型无关的,因此具有很高的可复用性。
- (3)它在编译时而不是运行时检查数据类型,保证了类型安全
- (4)它是平台无关的,可移植性
- (5)可用于基本数据类型
类模板 stack < type >
#include <cstdio>#include <string>#include<iostream>using namespace std;template<class Type>class Stack{ //类模板private: enum{MAX = 10}; Type items[MAX]; int top;public: Stack(); bool isEmpty(); bool isFull(); bool push(const Type & item); bool pop(Type& item); Type getTop();};template<class Type>Stack<Type>::Stack(){ top = 0;}template<class Type>bool Stack<Type>::isEmpty(){ return top == 0;}template<class Type>bool Stack<Type>::isFull(){ return top == MAX;}template<class Type>bool Stack<Type>::push(const Type & item){ if (top < MAX){ items[top++] = item; return true; } return false;}template<class Type>bool Stack<Type>::pop(Type & item){ if (top > 0){ item = items[--top]; return true; } return false;}template<class Type>Type Stack<Type>::getTop(){ return items[top-1];}int main(){ Stack<int> sta_i; // 实例化 int 模板类 sta_i.push(1); sta_i.push(2); int a = sta_i.getTop(); cout << a << endl; Stack<string> sta_d; // string sta_d.push("123"); sta_d.push("abc"); string d = sta_d.getTop(); cout << d << endl; Stack<char*> sta_cstar; // char* sta_cstar.push("asd"); printf("%s\n", sta_cstar.getTop()); return 0;}
成员模板
#include <cstdio>#include <string>#include<iostream>using namespace std;template <class T>class beta // nested template class member{private: template<typename V> class hold { private: V val; public: hold(V v = 0) :val(v){} void show() const { cout << val << endl; } V Value() const { return val; } }; hold<T> q; // template object hold<int> n; //template objectpublic: beta(T t, int i) :q(t), n(i){} template<typename U> // template method U blab(U u, T t){ return (n.Value() + q.Value()) * u / t; } void show() const { q.show(); n.show(); }};int main(){ beta<double> guy(3.5, 3); // T was set to double guy.show(); cout << guy.blab(10, 2.3) << endl; // return int cout << guy.blab(10.0, 2.3) << endl; // return double return 0;}
将模板作为参数
template <template <typename Type> class Thing> // Thing是个模板参数
#include <cstdio>#include <string>#include<iostream>using namespace std;template<class Type>class Stack{ //类模板private: enum{ MAX = 10 }; Type items[MAX]; int top;public: Stack(); bool isEmpty(); bool isFull(); bool push(const Type & item); bool pop(Type& item); Type getTop();};template<class Type>Stack<Type>::Stack(){ top = 0;}template<class Type>bool Stack<Type>::isEmpty(){ return top == 0;}template<class Type>bool Stack<Type>::isFull(){ return top == MAX;}template<class Type>bool Stack<Type>::push(const Type & item){ if (top < MAX){ items[top++] = item; return true; } return false;}template<class Type>bool Stack<Type>::pop(Type & item){ if (top > 0){ item = items[--top]; return true; } return false;}template<class Type>Type Stack<Type>::getTop(){ return items[top - 1];}template <template <typename Type> class Thing> // Thing是个模板参数class Crab{public: Thing<int> s1; Thing<double> s2;public: Crab(){}; bool push(int a, double x){ return s1.push(a) && s2.push(x); }};int main(){ Crab<Stack> sb; sb.push(1, 2.3); cout << sb.s1.getTop() << endl; cout << sb.s2.getTop() << endl; return 0;}
0 0
- 【c/c++】类模板
- 【C/C++】模板类
- 【c++】模板和模板类
- C/C++:函数模板与类模板
- [C/C++]模板函数与模板类
- c++vector模板类
- c++vector模板类
- c++:类模板
- [C++]类模板
- C++:类模板
- 【c++】类模板
- C++stack模板类
- 类模板与模板类(C++)
- C++,template,类模板和函数模板
- C++:函数模板、类模板及其特化
- C++---函数模板与类模板
- Linux C/C++ 模板:类模板(主模板)
- C/C++_log2000_函数模板与类模板笔记1
- C语言-运算符优先级
- catalog库配置
- java中自然排序和比较器排序
- 一座楼有10层,兔子可以跳跃的层数可以为1,2或者3,那么兔子到达10层有多少种跳法并列出各种情况。
- ora-12154无法解析指定的连接标识符
- C++ 类模板
- java.lang包学习
- PG 对比 Oracle
- Log4J的使用配置
- Python——插入排序
- SAP SQL 压缩 Database compression for SQL Server
- 【DP】[CodeForces - 713C]Sonya and Problem Wihtout a Legend
- class 和 struct
- Windows下使用jsoncpp