/* 模版成员模版 */ #include<iostream> using namespace std; template<typename V> class Value { public: Value(const V& v):m_v(v){} V m_v; }; template<typename K> class Key { public: Key(const K& k):m_k(k){} K m_k; template<typename V> class Value { public: Value(const V& v):m_v(v){} V m_v; }; }; int main() { Key<string> key("pi"); Key<string>::Value<double> value(3.1415); cout<<key.m_k<<" "<<value.m_v<<endl; return 0; }
/*
模版型成员变量
*/
#include<iostream>
using namespace std;
template<typename V>
class Value
{
public:
Value(const V& v):m_v(v){}
V m_v;
};
template<typename K,typename V>
class Pair
{
public:
Pair(const K& k,const V& v):m_k(k),m_v(v){}
K m_k;
Value<V> m_v;
};
int main()
{
Pair<string,double> pair("PI",3.14);
cout<<pair.m_v.m_v<<" "<<pair.m_k<<endl;
return 0;
}
#include<iostream> #include<cstring> using namespace std; //模板函数 template<typename T> T max(T x,T y) { return x>y?x:y; } //函数特化 template<> const char* max(const char * x,const char * y) { return strcmp(x,y)>0?x:y; } //通用模版类 template<typename T> class Comp { public: Comp(T x,T y):m_x(x),m_y(y){} T min()const { return m_x<m_y?m_x:m_y; } T max() const { return m_x>m_y?m_x:m_y; } private: T m_x; T m_y; }; /* //全类特化某种类型 template<> class Comp<const char*> { public: Comp(const char* x,const char * y):m_x(x),m_y(y){} const char* min() const { return strcmp(m_x,m_y)<0?m_x:m_y; } const char * max() const { return strcmp(m_x,m_y)>0?m_x:m_y; } private: const char* m_x; const char * m_y; }; */ //只特化成员函数 template<> const char* Comp<const char *>::min() const { return strcmp(m_x,m_y)<0?m_x:m_y; } //只特化成员函数 template<> const char * Comp<const char *>::max() const { return strcmp(m_x,m_y)>0?m_x:m_y; } int main() { cout<<::max<int>(45,33)<<endl; cout<<::max<string>("hello","world")<<endl; Comp<int> c1(34,20); cout<<c1.min()<<endl; Comp<const char *> c2("good","abc"); cout<<c2.min()<<endl; return 0; }
/* 模版实现输出流控制 */ #include<iostream> #include<sstream> using namespace std; template<typename T> class omanip { public: omanip(ostream&(*fun)(ostream&,T),T arg): m_fun(fun),m_arg(arg){} friend ostream& operator<<(ostream& os, const omanip<T>& om) { return om.m_fun(os,om.m_arg); } private: ostream& (*m_fun)(ostream&,T); T m_arg; }; class color { public: color(int ctrl,int bg,int fg):m_ctrl(ctrl) ,m_bg(bg),m_fg(fg){} int m_ctrl; int m_bg; int m_fg; }; ostream& colorfun(ostream& os,color args) { if(args.m_ctrl==-1) return os<<"\033[0m"; ostringstream oss; oss<<"\033["<<args.m_ctrl<<";" <<args.m_bg<<";" <<args.m_fg<<"m"; return os<<oss.str(); } omanip<color> colorfun1(int ctrl=-1,int bg=-1,int fg=-1) { return omanip<color>(colorfun,color(ctrl,bg,fg)); } int main() { cout<<colorfun1(1,44,33)<<"test color!" <<endl; cout<<colorfun1(-1,44,33)<<"test color!" <<endl; return 0; }
#include<iostream> using namespace std; //智能指针模版类 template<typename T> class smartptr { public: explicit smartptr(T* p=NULL):m_p(p){} smartptr(smartptr<T>& that):m_p(that.release()){} smartptr& operator=(smartptr<T> & that) { if(&that!=this) { reset(that.release()); return *this; } } T& operator*() const { return *m_p; } T* operator->() const { return &**this; } ~smartptr() { if(m_p) { delete m_p; m_p=NULL; } } private: T* release() { T* p=m_p; m_p=NULL; return p; } void reset(T* p) { if(p!=m_p) { delete m_p; m_p=p; } } T* m_p; }; //智能指错模版类,针对数组的特化 template<typename T> class smartptr<T[]> { public: explicit smartptr(T* p=NULL):m_p(p){} smartptr(smartptr<T>& that):m_p(that.release()){} smartptr& operator=(smartptr<T> & that) { if(&that!=this) { reset(that.release()); return *this; } } T& operator*() const { return *m_p; } T* operator->() const { return &**this; } ~smartptr() { if(m_p) { delete[] m_p; m_p=NULL; } } private: T* release() { T* p=m_p; m_p=NULL; return p; } void reset(T* p) { if(p!=m_p) { delete[] m_p; m_p=p; } } T* m_p; }; class stu { public: stu(int id=0,const char * name=""):id(id),name(name){ cout<<"stu()\n"; } ~stu() { cout<<"~stu()\n"; } void who() const { cout<<"id:"<<id<<",name:"<<name<<endl; } private: string name; int id; }; int main() { smartptr<stu> ps(new stu(1,"zhansan")); ps->who(); smartptr<stu[]> pss(new stu[5]); return 0; }