c++知識點總結--函數模板

來源:互聯網
上載者:User

標籤: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++知識點總結--函數模板

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.