標籤:for array 模板 blog 類型 參數 知識點 最佳化 const
通用函數可變參模板
用於處理不限定參數的函數 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]<<" "; }}
c++知識點總結--函數模板