當(dāng)前位置:
首頁 >
C++(十)——模板(上)
發(fā)布時間:2025/3/21
31
豆豆
生活随笔
收集整理的這篇文章主要介紹了
C++(十)——模板(上)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
函數(shù)模板基本語法
#include<iostream> using namespace std;//兩個整型交換函數(shù) //函數(shù)模板 template<typename T> //聲明模板void mySwap(T& a, T& b) {T temp = a;a = b;b = temp; } void test01() {int a = 10;int b = 20;//自動類型推導(dǎo)//mySwap(a, b);//顯示指定類型mySwap<int>(a, b);cout << "a = " << a << endl;cout << "b = " << b << endl; } int main() {test01(); }函數(shù)模板注意事項(xiàng)
#include<iostream> using namespace std; template<class T>//typename可以替換成class void mySwap(T& a, T& b) {T temp = a;a = b;b = temp; }//函數(shù)模板注意事項(xiàng) //1、自動類型推導(dǎo),必須推導(dǎo)出一致的數(shù)據(jù)類型T才可以使用 void test01() {int a = 10;int b = 20;char c = 'c';//mySwap(a, b);//mySwap(a, c);推導(dǎo)不出一致的T類型cout << "a = " << a << endl;cout << "b = " << b << endl; } //2、模板必須要確定出T的數(shù)據(jù),才可以使用 template <class T> void func() {cout << "fuc的調(diào)用" << endl; } void test02() {func<int>(); }int main() {test01();test02();system("pause");return 0; }函數(shù)模板案例
#include<iostream> using namespace std; //實(shí)現(xiàn)一個通用的對數(shù)組的排序模板函數(shù) template<typename T> void sort(T a[], int n) {for (int i = 0; i < n - 1 ; ++i) {int k = i;for (int j = k + 1; j < n; ++j) {if (a[j] < a[k]) {k = j;}}if (k != i) {T t = a[k];a[k] = a[i];a[i] = t;}} }template<typename T> void printArray(T a[], int n) {for (int i = 0; i < 10; ++i) {cout << a[i] << endl;}cout << endl; }void test01() {int a[10] = { 4,2,1,5,3,7,8,10,6,9 };sort(a, 10);//printArray(a, 10);char c[] = "safjsfjid";int num = sizeof(c) / sizeof(char);sort(c, num);printArray(c, num); }int main() {test01();return 0; }普通函數(shù)與函數(shù)模板區(qū)別
#include<iostream> using namespace std; //普通函數(shù)與函數(shù)模板的區(qū)別 //普通函數(shù)調(diào)用可以發(fā)生隱式類型轉(zhuǎn)換 //函數(shù)模板用顯示指定類型,可以發(fā)生隱式類型轉(zhuǎn)換 //函數(shù)模板用自動類型推導(dǎo),不可以發(fā)生隱式類型轉(zhuǎn)換//普通函數(shù) int myAdd01(int a, int b) {return a + b; } //函數(shù)模板 template<typename T> T myAdd02(T a, T b) {return a + b; } void test01() {int a = 10;int b = 20;char c = 'c';cout << myAdd01(a, c) << endl;//自動類型推導(dǎo) 不會發(fā)生隱式類型轉(zhuǎn)換//cout << myAdd02(a, c) << endl;//顯示指定類型cout << myAdd02<int>(a, c) << endl;}int main() {test01();return 0; }普通函數(shù)與函數(shù)模板調(diào)用規(guī)則
#include<iostream> using namespace std;//普通函數(shù)與函數(shù)模板的調(diào)用規(guī)則 //1 函數(shù)模板與普通函數(shù)同名 優(yōu)先調(diào)用普通函數(shù) //2 可以通過空模板參數(shù)列表 強(qiáng)制調(diào)用 函數(shù)模板 //3 函數(shù)模板可以發(fā)生函數(shù)重載 //4 如果函數(shù)模板可以產(chǎn)生更好的匹配,優(yōu)先調(diào)用函數(shù)模板 void myPrint(int a, int b) {cout << "調(diào)用普通函數(shù)" << endl; }template<typename T> void myPrint(T a, T b) {cout << "調(diào)用模板函數(shù)" << endl; }template<typename T> void myPrint(T a, T b, T c) {cout << "調(diào)用重載模板函數(shù)" << endl; }void test01() {myPrint(1, 3);//通過空模板的參數(shù)列表,強(qiáng)制調(diào)用函數(shù)模板myPrint<>(1, 3);//模板函數(shù)也能重載myPrint(1, 2, 3);//如果函數(shù)模板產(chǎn)生更好的匹配優(yōu)先調(diào)用函數(shù)模板myPrint('1', '2'); }int main() {test01();return 0; }模板的局限性
#include<iostream> using namespace std; //模板的局限性 //模板并不是萬能的,有些特定數(shù)據(jù)類型,需要具體化方式做特殊實(shí)現(xiàn)class Person { public:string m_name;int m_age;Person(string name, int age) {m_name = name;m_age = age;} };//對比兩個數(shù)據(jù)是否相等函數(shù) template<class T> bool myCompare(T& a, T& b) {if (a == b)return true;return false;}//利用具體化Person的版本實(shí)現(xiàn)代碼,具體化優(yōu)先調(diào)用 template<> bool myCompare(Person& p1, Person& p2) {if (p1.m_name == p2.m_name && p1.m_age == p2.m_age)return true;return false; }void test02() {Person p1("tom", 12);Person p2("tom", 132);bool ret = myCompare(p1, p2);if (ret)cout << "p1等于p2" << endl;elsecout << "p1不等于p2" << endl; }void test01() {int a = 10;int b = 20;bool ret = myCompare(a, b);if (ret)cout << "a等于b" << endl;elsecout << "a不等于b" << endl;}int main() {//test01();test02();return 0; }函數(shù)模板案例
#include<iostream> using namespace std; //實(shí)現(xiàn)一個通用的對數(shù)組的排序模板函數(shù) template<typename T> void sort(T a[], int n) {for (int i = 0; i < n - 1 ; ++i) {int k = i;for (int j = k + 1; j < n; ++j) {if (a[j] < a[k]) {k = j;}}if (k != i) {T t = a[k];a[k] = a[i];a[i] = t;}} }template<typename T> void printArray(T a[], int n) {for (int i = 0; i < 10; ++i) {cout << a[i] << endl;}cout << endl; }void test01() {int a[10] = { 4,2,1,5,3,7,8,10,6,9 };sort(a, 10);//printArray(a, 10);char c[] = "safjsfjid";int num = sizeof(c) / sizeof(char);sort(c, num);printArray(c, num); }int main() {test01();return 0; }總結(jié)
以上是生活随笔為你收集整理的C++(十)——模板(上)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C++(九)——职工信息管理系统
- 下一篇: 深度探索C++ 对象模型(1)-三种对象