zoukankan      html  css  js  c++  java
  • c++知识点总结--函数模板

    通用函数可变参模板

    用于处理不限定参数的函数 
    showall(){//空函数,接口,最后结束递归
    }
    template<typename T,typename... Args>
    void showall(T value,Args ...args){
      cout<<value<<endl;
      showall(args);
    }
    template<typename T,typename ...Args>
    void showall(const T &value,const Args &...args){
    
    }
    //设计可以修改原来的数据  T &value,Args &...args
    //设计不可以修改原来的数据可以修改副本 T value,Args ...args
    //设计不可以修改原来的数据不可以修改副本 const T value,const Args ...args

    函数模板的覆盖

    结构体可以直接赋值,所有成员都是公有的类也可直接赋值
    
    struct info{
    char name[40];
    double db;
    int data;
    }
    template <typename T>
    void     swap(T&a,T&b){
      cout<<"通用函数模板"<<endl;
      T temp=a;
      a=b;
      b=temp;
    }    
    template <>//模板为空,指定类型
    void     swap(info&a,info&b){
      cout<<"特有函数模板"<<endl;
      //根据自己的数据类型进行优化
      T temp=a;
      a=b;
      b=temp;
    }    

    函数模板的重载

    template<typename T>
    void showarray(array<T,10> myarray,int n){
        cout<<"func 1"<<endl;
        for(int i=0;i<n;i++){
            cout<<myarray[i]<<" ";
        }
    }
    void showarray(array<T*,10> myarray,int n){
        cout<<"func 2"<<endl;
        for(int i=0;i<n;i++){
            cout<<*myarray[i]<<" ";
        }
    }    
  • 相关阅读:
    IE、chrome、火狐中如何调试javascript脚本
    RFS_oracle的操作
    python_操作oracle数据库
    RFS_窗口或区域之间的切换
    RFS_关键字
    python_遇到问题
    python_GUI
    python_文件
    python之深浅拷贝
    python之编码和解码
  • 原文地址:https://www.cnblogs.com/tla001/p/6702047.html
Copyright © 2011-2022 走看看