zoukankan      html  css  js  c++  java
  • 模版

    • 非类型模版
    • 类型模版
      1. 类模版
      2. 函数模版

    一。非类型模版 

    #include <iostream>
    using namespace std;
    
    template<int Size>
    class Index
    {
        public:
            int operator[](char vchar) 
            { return vchar % Size; }
    };
    
    int main()
    {
        Index<26> index;
        cout << index['a'] << endl;
    }

    二。类模版

    模版——模版形参为基本类型

    #include <iostream>
    using namespace std;
    
    template<class Type>
    class trie
    {
        public:
            trie(Type val_1, Type val_2) : num_1(val_1), num_2(val_2) {}
            void print_sum() { cout << num_1 + num_2 << endl; }
        private:
            Type num_1, num_2;
    };
    
    int main()
    {
        trie<int> tt(2, 5);
        tt.print_sum();
    }

    模版——模版形参为自定义非模版类

    #include <iostream>
    using namespace std;
    
    template<class Type>
    class trie
    {
        public:
            Type num;
    };
    class Index
    {
        public:
            int operator[](char vchar) 
            { return vchar % 26; }
    };
    
    int main()
    {
        trie<Index> tt;
        cout << tt.num['a'] << endl;
        cout << tt.num['z'] << endl;
    }

    模版——模版形参为自定义模版类

    #include <iostream>
    using namespace std;
    
    template<class Type, int Size>
    class trie
    {
        public:
            Type num;
    };
    
    template<int Size>
    class Index
    {
        public:
            int operator[](char vchar) 
            { return vchar % Size; }
    };
    
    int main()
    {
        trie<Index<26>, 26> t;
    }

    类模版成员函数定义

    成员函数为非模版函数

    #include <iostream>
    #include <cstring>
    using namespace std;
    
    template <class Type, int Size>
    class test
    {
        public:
            test(Type num) : val(num) {}
            void print_val();
        private:
            Type val;
    };
    
    template<class Type, int Size>
    void test<Type, Size>::print_val()
    {
        cout << val << " " << Size << endl;
    }
    
    int main()
    {
        test<int, 10> a(4);
        a.print_val();
    }

    成员函数为模版函数

    #include <iostream>
    #include <cstring>
    using namespace std;
    
    template <class Type, int Size>
    class test
    {
        public:
            test(Type num) : val(num) {}
            template<class Type_fuc>
            void print_val(Type_fuc m);
        private:
            Type val;
    };
    
    template<class Type, int Size>
    template<class Type_fuc>  //模版类的模版函数需要另起一行
    void test<Type, Size>::print_val(Type_fuc m)
    {
        cout << val << " " << Size << endl;
        cout << "m:" << m << endl;
    }
    
    int main()
    {
        test<int, 10> a(4);
        a.print_val("hello");
    }

    三。函数模版

    #include <iostream>
    using namespace std;
    
    template<class Type>
    Type sum(Type num_1, Type num_2)
    {
        return num_1 + num_2;
    }
    int main()
    {
        cout << sum(2, 3) << endl;
        cout << sum(2.2, 3.0) << endl;
    }

    结果

    5

    5.2

  • 相关阅读:
    ant
    maven 构建web项目
    什么叫openapi
    dubbo学习
    Java 获取环境变量
    配置文件书写对象的几种方式
    怎么改svn的登陆账号
    Android 它们的定义View (一)
    eclipse建立cocos2d-x开发环境
    Android——采用SQLiteDatabase操作SQLite数据库
  • 原文地址:https://www.cnblogs.com/kaituorensheng/p/3601495.html
Copyright © 2011-2022 走看看