zoukankan      html  css  js  c++  java
  • C++模板函数实践1

    实践如下:

    #include <iostream>
    #include <typeinfo>
    
    using namespace std;
    
    class Bean{
    private:
        int a;
    public:
        Bean(){
            this->a = 0;
        }
        Bean(int a){
            this->a = a;
        }
        ~Bean(){}
        int getA(){
            return a;
        }
        // 重载==操作符
        bool operator==(Bean to){
            cout<<"Bean重载操作符==进行比较"<<endl;
            return this->a == to.a;
        }
        bool operator>(Bean to){
            cout<<"Bean重载操作符>进行比较"<<endl;
            return this->a > to.a;
        }
        friend ostream & operator<<(ostream &out, Bean obj){
            cout<<"bean a值:"<< obj.a<<endl;
            return out;
        }
    };
    
    // 模板方法实践
    template<class Type>
    Type maxV(Type a, Type b){
        //cout<<" typeid(a).name(): "<< typeid(a).name()<<endl;
        return a > b ? a : b;
    }
    // 重载方法
    int maxV(int a, int b){
        cout<<" 重载函数"<<endl;
        return a > b ? a : b;
    }
    
    template<class K, class V>
    class MyMap{
    private:
        K keys[10];
        V values[10];
        int count = 0;
    public:
        void put(K key, V value){
            keys[count] = key;
            values[count] = value;
            count++;
        }
        V get(K key){
    //        if(key == NULL){
    //            cout << "入参key为空,返回NULL" << endl;
    //            return (V)NULL;
    //        }
            for(int i = 0; i< count; i++){
                if(keys[i] == key){
                    cout << "命中下标:" << i << endl;
                    return values[i];
                }
            }
            cout << "找不到key,返回NULL" << endl;
            return (V)NULL;
        }
    };
    
    
    int main() {
    
        cout << "模板函数1 实践:" << endl;
    
        cout<<"maxV(11,220): "<<maxV(11,220)<<endl;
        cout<<"maxV(11.1f,220.1f): "<<maxV(11.1f,220.1f)<<endl;
        cout<<"max(11.1L,220.1L): "<<maxV(11.1L,220.1L)<<endl;
        cout<<"max('a','c'): "<<maxV('a','c')<<endl<<endl;
    
        MyMap<int,double> map;
        map.put(11, 100);
        map.put(22, 200);
        map.put(33, 333);
        map.put(44, 444);
        cout<<"map.get(1): "<<map.get(11)<<endl;
        cout<<"map.get(44): "<<map.get(44)<<endl;
        cout<<"map.get(55): "<<map.get(55)<<endl;
        cout<<"map.get(1): "<<map.get((int)NULL)<<endl;
    
    
        MyMap<Bean,int> beanMap;
        beanMap.put(Bean(1), 11111);
        beanMap.put(Bean(11), 11);
        beanMap.put(Bean(111), 111);
        cout<<"beanMap.get(Bean(1)): "<<beanMap.get(Bean(1))<<endl;
    
        cout<<"maxV(Bean(1),Bean(11)): "<<maxV(Bean(1),Bean(11))<<endl<<endl;
    
    
        cout << "
    模板函数 end." << endl;
    
        return 0;
    }

    输出结果:

  • 相关阅读:
    包含min函数的栈
    量化交易系统的四个组成部分
    顺时针打印矩阵
    python正则表达式中re.M,re.S,re.I的作用
    二叉树的镜像
    树的子结构
    合并两个排序的链表
    反转链表
    命名元祖
    二叉树(二叉搜索树-AVL树-B树)
  • 原文地址:https://www.cnblogs.com/do-your-best/p/11216592.html
Copyright © 2011-2022 走看看