深度解析C++的函式模板與類別模板
一、函數模板
1、定義
template
傳回值型別範本名(形參表){
函數體
};
template <class T1, class T2> T2 print(T1 arg1, T2 arg2) { cout<< arg1 << " "<< arg2<<endl; return arg2; }
2、不以參數實例化函數範本
#include <iostream> using namespace std; template <class T> T Inc(T n){ return 1 + n; } int main(){ cout << Inc<double>(4)/2; //输出 2.5 return 0; }
3、函數模板可以重載,只要它們的形參表或類型參數表不同即可
template<class T1, class T2> void print(T1 arg1, T2 arg2) { cout<< arg1 << " "<< arg2<<endl; } template<class T> void print(T arg1, T arg2) { cout<< arg1 << " "<< arg2<<endl; } template<class T,class T2> void print(T arg1, T arg2) { cout<< arg1 << " "<< arg2<<endl; }
4、函數模板和函數的順序
在有多個函數和函數模板名字相同的情況下,編譯器如下處理一條函數呼叫語句
先找參數完全匹配的普通函數(非由模板實例化而得的函數)。
再找參數完全符合的模板函數。
再找實參數經過自動型別轉換後能夠匹配的普通函數。
上面的都找不到,則報錯。
template <class T> T Max( T a, T b) { cout << "TemplateMax" <<endl; return 0; } template <class T,class T2> T Max( T a, T2 b) { cout << "TemplateMax2" <<endl; return 0; } double Max(double a, double b){ cout << "MyMax" << endl; return 0; } int main() { Max( 1.2,3.4); // 输出MyMax Max(4, 5); //输出TemplateMax Max( 1.2, 3); //输出TemplateMax2 return 0; }
5、符合範本函數時,不進行型別自動轉換
template<class T> T myFunction( T arg1, T arg2) { cout<<arg1<<" "<<arg2<<"\n"; return arg1;} …… myFunction( 5, 7); //ok :replace T with int myFunction( 5.8, 8.4); //ok: : replace T with double myFunction( 5, 8.4); //error ,no matching function for callto 'myFunction(int, double)'
二、類別範本
1、定義
在定義類別的時候,加上一個/多個型別參數。在使用類別模板時,指定類型參數應該如何替換成具體類型,編譯器據此產生對應的模板類別。
template
class 類模板名{
成員函數和成員變量
};
(1)類別模板裡成員函數的寫法:
template
傳回值型別範本名稱<型別參數名稱列表>::成員函式名稱(參數表){
…
}
(2)使用類別範本定義物件的寫入:
##類別模板名稱<真實類型參數表> 物件名稱(建構子實參表);
// Pair类模板 template <class T1,class T2> class Pair{ public: T1 key; //关键字 T2 value; //值 Pair(T1 k,T2 v):key(k),value(v) { }; bool operator < ( const Pair<T1,T2> & p) const; }; template<class T1,class T2> bool Pair<T1,T2>::operator < ( const Pair<T1,T2> & p) const{ //Pair的成员函数 operator < return key < p.key; } int main(){ Pair<string,int> student("Tom",19); //实例化出一个类 Pair<string,int> cout << student.key << " " << student.value; return 0; } //输出: Tom 19
2、用類別模板定義物件
template <class T> class A{ public: template<class T2> void Func( T2 t) { cout << t; } //成员函数模板 };
template <class T, int size> class CArray{ T array[size]; public: void Print(){ for( int i = 0;i < size; ++i) cout << array[i] << endl; } }; CArray<double,40> a2; CArray<int,50> a3; //a2和a3属于不同的类
template <class T1,class T2> int main() { class A { B<int,double> obj1; T1 v1; T2 v2; C<int> obj2; }; return 0; template <class T1,class T2> } class B:public A<T2,T1> { class B<int,double>: T1 v3; T2 v4; public A<double,int>{ }; int v3; double v4; template <class T> }; class C:public B<T,T> { T v5; };
template <class T1,class T2> class A { T1 v1; T2 v2; }; template <class T> class B:public A<int,double> { T v; }; int main() { B<char> obj1; //自动生成两个模板类 :A<int,double> 和 B<char> return 0; }
class A { int v1; }; template <class T> class B:public A { //所有从B实例化得到的类 ,都以A为基类 T v; }; int main() { B<char> obj1; return 0; }
template <class T> class A { T v1; int n; }; class B:public A<int> { double v; }; int main() { B obj1; return 0; }
void Func1() { } class A { }; class B{ public: void Func() { } }; template <class T> class Tmpl{ friend void Func1(); friend class A; friend void B::Func(); }; //任何从Tmp1实例化来的类 ,都有以上三个友元
#include <iostream> #include <string> using namespace std; template <class T1,class T2> class Pair{ private: T1 key; //关键字 T2 value; //值 public: Pair(T1 k,T2 v):key(k),value(v) { }; bool operator < ( const Pair<T1,T2> & p) const; template <class T3,class T4> friend ostream & operator<< ( ostream & o,const Pair<T3,T4> & p); }; template <class T1,class T2> bool Pair<T1,T2>::operator < ( const Pair<T1,T2> & p) const{ //"小"的意思就是关键字小 return key < p.key; } template <class T1,class T2> ostream & operator<< (ostream & o,const Pair<T1,T2> & p){ o << "(" << p.key << "," << p.value << ")" ; return o; } int main() { Pair<string,int> student("Tom",29); Pair<int,double> obj(12,3.14); cout << student << " " << obj; return 0; } //输出: (Tom,29) (12,3.14) 任意从 template <class T1,class T2> ostream & operator<< (ostream & o,const Pair<T1,T2> & p) 生成的函数,都是任意Pair摸板类的友元
#include <iostream> using namespace std; class A { int v; public: A(int n):v(n) { } template <class T> friend void Print(const T & p); }; template <class T> void Print(const T & p){ cout << p.v; } int main() { A a(4); Print(a); return 0; } //输出:4
template <class T> class B { T v; public: B(T n):v(n) { } template <class T2> friend class A; }; template <class T> class A { public: void Func( ) { B<int> o(10); cout << o.v << endl; } };
#include <iostream> using namespace std; template <class T> class A{ private: static int count; public: A() { count ++; } ~A() { count -- ; }; A( A & ) { count ++ ; } static void PrintCount() { cout << count << endl; } }; template<> int A<int>::count = 0; template<> int A<double>::count = 0; int main(){ A<int> ia; A<double> da; ia.PrintCount(); da.PrintCount(); return 0; } //输出:1 1
以上是深度解析C++的函式模板與類別模板的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

熱門話題

C語言中通過轉義序列處理特殊字符,如:\n表示換行符。 \t表示製表符。使用轉義序列或字符常量表示特殊字符,如char c = '\n'。注意,反斜杠需要轉義兩次。不同平台和編譯器可能有不同的轉義序列,請查閱文檔。

在 C 語言中,char 類型在字符串中用於:1. 存儲單個字符;2. 使用數組表示字符串並以 null 終止符結束;3. 通過字符串操作函數進行操作;4. 從鍵盤讀取或輸出字符串。

C 語言中符號的使用方法涵蓋算術、賦值、條件、邏輯、位運算符等。算術運算符用於基本數學運算,賦值運算符用於賦值和加減乘除賦值,條件運算符用於根據條件執行不同操作,邏輯運算符用於邏輯操作,位運算符用於位級操作,特殊常量用於表示空指針、文件結束標記和非數字值。

在 C 語言中,char 和 wchar_t 的主要區別在於字符編碼:char 使用 ASCII 或擴展 ASCII,wchar_t 使用 Unicode;char 佔用 1-2 個字節,wchar_t 佔用 2-4 個字節;char 適用於英語文本,wchar_t 適用於多語言文本;char 廣泛支持,wchar_t 依賴於編譯器和操作系統是否支持 Unicode;char 的字符範圍受限,wchar_t 的字符範圍更大,並使用專門的函數進行算術運算。

多線程和異步的區別在於,多線程同時執行多個線程,而異步在不阻塞當前線程的情況下執行操作。多線程用於計算密集型任務,而異步用於用戶交互操作。多線程的優勢是提高計算性能,異步的優勢是不阻塞 UI 線程。選擇多線程還是異步取決於任務性質:計算密集型任務使用多線程,與外部資源交互且需要保持 UI 響應的任務使用異步。

在 C 語言中,char 類型轉換可以通過:強制類型轉換:使用強制類型轉換符將一種類型的數據直接轉換為另一種類型。自動類型轉換:當一種類型的數據可以容納另一種類型的值時,編譯器自動進行轉換。

char 數組在 C 語言中存儲字符序列,聲明為 char array_name[size]。訪問元素通過下標運算符,元素以空終止符 '\0' 結尾,用於表示字符串終點。 C 語言提供多種字符串操作函數,如 strlen()、strcpy()、strcat() 和 strcmp()。

C語言中沒有內置求和函數,需自行編寫。可通過遍歷數組並累加元素實現求和:循環版本:使用for循環和數組長度計算求和。指針版本:使用指針指向數組元素,通過自增指針遍歷高效求和。動態分配數組版本:動態分配數組並自行管理內存,確保釋放已分配內存以防止內存洩漏。
