C++中模板(Template)詳解及其作用介紹
概述
模板可以幫助我們提高代碼的可用性, 可以幫助我們減少開(kāi)發(fā)的代碼量和工作量.

函數(shù)模板
函數(shù)模板 (Function Template) 是一個(gè)對(duì)函數(shù)功能框架的描述. 在具體執(zhí)行時(shí), 我們可以根據(jù)傳遞的實(shí)際參數(shù)決定其功能. 例如:
int max(int a, int b, int c){
a = a > b ? a:b;
a = a > c ? a:c;
return a;
}
long max(long a, long b, long c){
a = a > b ? a:b;
a = a > c ? a:c;
return a;
}
double max(double a, double b, double c){
a = a > b ? a:b;
a = a > c ? a:c;
return a;
}
寫(xiě)成函數(shù)模板的形式:
template<typename T>
T max(T a, T b, T c){
a = a > b ? a:b;
a = a > c ? a:c;
return a;
}
類(lèi)模板
類(lèi)模板 (Class Template) 是創(chuàng)建泛型類(lèi)或函數(shù)的藍(lán)圖或公式.
#ifndef PROJECT2_COMPARE_H
#define PROJECT2_COMPARE_H
template <class numtype> // 虛擬類(lèi)型名為numtype
class Compare {
private:
numtype x, y;
public:
Compare(numtype a, numtype b){x=a; y=b;}
numtype max() {return (x>y)?x:y;};
numtype min() {return (x < y)?x:y;};
};
mian:
int main() {
Compare<int> compare1(3,7);
cout << compare1.max() << ", " << compare1.min() << endl;
Compare<double> compare2(2.88, 1.88);
cout << compare2.max() << ", " << compare2.min() << endl;
Compare<char> compare3('a', 'A');
cout << compare3.max() << ", " << compare3.min() << endl;
return 0;
}
輸出結(jié)果:
7, 3
2.88, 1.88
a, A
模板類(lèi)外定義成員函數(shù)
如果我們需要在模板類(lèi)外定義成員函數(shù), 我們需要在每個(gè)函數(shù)都使用類(lèi)模板. 格式:
template<class 虛擬類(lèi)型參數(shù)>
函數(shù)類(lèi)型 類(lèi)模板名<虛擬類(lèi)型參數(shù)>::成員函數(shù)名(函數(shù)形參表列) {}
類(lèi)模板:
#ifndef PROJECT2_COMPARE_H
#define PROJECT2_COMPARE_H
template <class numtype> // 虛擬類(lèi)型名為numtype
class Compare {
private:
numtype x, y;
public:
Compare(numtype a, numtype b);
numtype max();
numtype min();
};
template<class numtype>
Compare<numtype>::Compare(numtype a,numtype b) {
x=a;
y=b;
}
template<class numtype>
numtype Compare<numtype>::max( ) {
return (x>y)?x:y;
}
template<class numtype>
numtype Compare<numtype>::min( ) {
return (x>y)?x:y;
}
#endif //PROJECT2_COMPARE_H
類(lèi)庫(kù)模板
類(lèi)庫(kù)模板 (Standard Template Library). 例如:
#include <vector>
#include <iostream>
using namespace std;
int main() {
int i = 0;
vector<int> v;
for (int i = 0; i < 10; ++i) {
v.push_back(i); // 把元素一個(gè)一個(gè)存入到vector中
}
for (int j = 0; j < v.size(); ++j) {
cout << v[j] << " "; // 把每個(gè)元素顯示出來(lái)
}
return 0;
}
輸出結(jié)果:
0 1 2 3 4 5 6 7 8 9
抽象和實(shí)例

到此這篇關(guān)于C++中模板(Template)詳解及其作用介紹的文章就介紹到這了,更多相關(guān)C++模板內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
QT調(diào)用vs2019生成的c++動(dòng)態(tài)庫(kù)的方法實(shí)現(xiàn)
本文主要介紹了QT調(diào)用vs2019生成的c++動(dòng)態(tài)庫(kù)的方法實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-06-06
C語(yǔ)言結(jié)構(gòu)及隊(duì)列實(shí)現(xiàn)示例詳解
這篇文章主要為大家介紹了C語(yǔ)言實(shí)現(xiàn)隊(duì)列示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12
C/C++?Qt?Dialog?對(duì)話(huà)框組件應(yīng)用技巧
這篇文章主要介紹了C/C++?Qt?Dialog?對(duì)話(huà)框組件應(yīng)用,這里我將總結(jié)本人在開(kāi)發(fā)過(guò)程中常用到的標(biāo)準(zhǔn)對(duì)話(huà)框的使用技巧,對(duì)C++?對(duì)話(huà)框組件相關(guān)知識(shí)感興趣的朋友一起看看吧2021-11-11
C++實(shí)現(xiàn)AVL樹(shù)的基本操作指南
AVL樹(shù)是高度平衡的而二叉樹(shù),它的特點(diǎn)是AVL樹(shù)中任何節(jié)點(diǎn)的兩個(gè)子樹(shù)的高度最大差別為1,下面這篇文章主要給大家介紹了關(guān)于C++實(shí)現(xiàn)AVL樹(shù)的相關(guān)資料,需要的朋友可以參考下2022-01-01

