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