C 筆記 04C 模版1

2021-08-07 08:07:53 字數 3809 閱讀 2978

#include

using namespace std;

/*oop三大特徵

封裝  繼承 多型

第二部分  模版

《c++ templates》

1.函式模版

2.類模板

需要搞清楚的知識點

函式模版

模版的例項化,(顯示, 隱式)

模版函式

模版的實參推演

模版型別引數

模版的非型別引數

模版的特例化(專用化)

模版函式的過載

*//*

int sum(int a,int b);  a,b 實參的值進行引數化

模版就是將型別也進行引數化

*//*

*1*模版的**內容例項化之前不進行編譯

*2*因為1 ,所以當它沒有任何的例項化(也就是沒有產生任何函式),所以,對於模版的**不能寫到頭檔案裡,

所以,在模版的呼叫處,一定要看到模版的定義處,光宣告不可以

想在標頭檔案裡寫,就必須顯示例項化

*///語法:定義模版的引數列表    可定義   型別引數/非型別引數  class/typename

//template//類型別

//template//非類型別引數

//template//可定義多個,一般都是大寫

#if 1

/*類模版

鍊錶*/

//前向宣告

template

class clink;

//類模版 除了構造和析構函式的名字 可以省略引數列表(模版的引數列表),其他地方不能省略

template//node

class node

;private:

t _data;

node*_pnext;

//template//clinkclinkclink

//friend class clink;

friend class clink;//宣告模版clink,,t特例化,,要是用這個得在前面進行前向宣告

};//可以看出,不同模版的引數名可以用在不同模版出,也就是相同的模版引數名,不在同一作用域處就可以重名

template

class clink

~clink()

}void inserthead(const t &val);

void show()

cout<}

private:

//作業:  寫 巢狀類  class node

node*_phead;//node是乙個模版名,但是指標前面需要型別,在這裡模版不在它的定義體內,所以需要乙個模版引數來例項化

};template

void clink::inserthead(const t &val)

int main()

#endif

#if 0

//函式模版不支援給預設值,但是類模板支援

template

int findvalindex(t *array,int length,const t &val)

}return -1;

}//char *特例化

template<>

int findvalindex(char **array,int length,char *const &val)

}return -1;

}int main()

;int length = sizeof(array)/sizeof(array[0]);

int index = -1;

index = findvalindex(array,length,78);

if(index == -1)

cout<<"78 index is:"length = sizeof(strarr)/sizeof(strarr[0]);

index = findvalindex(strarr,length,"sdafs");

if(index == -1)

cout<<"sdafs index is:"}#endif

#if 0

/* 模版的特例化(專用化) */

template

bool compare(t a,t b)

/*對於特例化,應當提供該特例化(專用化)的處理版本

對於函式來說,  只支援模版的完全特例化,不支援模版的部分特例化(只有類模版才支援)

*/template<>

bool compare(char *a,char *b)

template<>

bool compare(int a,int b)

template<>

bool compare(const int a,const int b)

//非模版函式

//函式模版 模版的特例化 非模版函式   可以共從,,   呼叫順序, 非模版函式->模版的特例化->模版函式

bool compare(int a,int b)

int main()

#endif

#if 0

template

bool compare(const t a,const t b)//此處const修飾的是a,b變數名

template<>

bool compare(char * const a,char * const b)//此處const修飾的是a,b變數名,所以特例化的時候const要修飾a ,b

/*#define t int

typedef int t;

const t a;

#define t char *

typedef char *t;

const t b;

*/template//int size 是非模版型別引數,不能作為左值, 本身是常量,所以傳入必須的常量

void sort(t arr)}}

}template void sort(int arr,int size);//特例化

template//templatetemplatetemplatetemplate

void func()

//氣泡排序

template

void sort(t arr,int n)}}

}int main()

//template void sort( int arr,int n);   //顯示例項化,當函式呼叫點和函式定義不處於同乙個檔案中

//sort(array ,10 )

//常量與常變數

const int size = sizeof(array)/sizeof(array[0]);//常量  

//int length = sizeof(array)/sizeof(array[0]);

//const int size = length;//常變數沒有給乙個明確的初始值

sort(array,size);

for(int i=0;i<10;++i)

cout<

bool compare(t a,t b)

//template

//bool compare(t a,e b)

////函式模版 -> 例項化 -> 模版函式

/*//模版函式

bool compare(int a,int b)

*///顯示例項化

template bool compare(int,int);

template bool compare(double,double);

template bool compare(float,float);

int main()

#endif

C 系列筆記 04 C 泛型程式設計

類模板模板中的繼承 模板是泛型的實現,泛型是模板的思想。泛型的本質是型別的引數化。注意!不傳參時必須顯式指定型別,看下面的例子。template class t void function int main 函式模板與普通函式的區別 呼叫順序 過載時,優先呼叫普通函式 template class ...

04 C語言基本語法

c 語言的程式 由各種令牌組成,令牌可以是關鍵字 識別符號 常量 字串值,或者是乙個符號。例如,下方的c語句包括5個令牌 printf hello,world n 5個令牌分別是 printf hello,world n c語言中,每乙個語句都必須以分號結束。它表明乙個邏輯實體的結束。分號是語句結束...

04 C 筆記 資料型別轉化

支援強制型別轉換。常用的轉化函式如下 1toboolean 如果可能的話,把型別轉換為布林型。2tobyte 把型別轉換為位元組型別。3tochar 如果可能的話,把型別轉換為單個 unicode 字元型別。4todatetime 把型別 整數或字串型別 轉換為 日期 時間 結構。5todecima...