WTL 簡單的模板類陣列研究

2021-06-21 08:30:41 字數 3577 閱讀 1796

實際上這裡談到的類是atl裡面的類,位於標頭檔案: atlsimpcoll.h

如果你去探尋,就會發現wtl實際上就是atl,只不過它擴充套件了atl的介面功能。wtl當然有許多自己的東西,但那是另外的問題了。

template >

class c******array

~c******array();

c******array(_in_ const c******array< t, tequal >& src) :

m_at(null), m_nsize(0), m_nallocsize(0)

else

for (int i=0; i= (m_at + m_nallocsize) ) );

t* at;

int nnewallocsize = (m_nallocsize == 0) ? 1 : (m_nsize * 2);

if (nnewallocsize<0||nnewallocsize>int_max/sizeof(t))

at = (t*)_recalloc(m_at, nnewallocsize, sizeof(t));

if(at == null)

return false;

m_nallocsize = nnewallocsize;

m_at = at;

} internalsetatindex(m_nsize, t);

m_nsize++;

return true;

} bool remove(_in_ const t& t)

bool removeat(_in_ int nindex)

void removeall()

m_nsize = 0;

m_nallocsize = 0;

}const t& operator (_in_ int nindex) const

return m_at[nindex];

} t& operator (_in_ int nindex)

return m_at[nindex];

} t* getdata() const

int find(_in_ const t& t) const

return -1; // not found

} bool setatindex(

_in_ int nindex,

_in_ const t& t)

// implementation

template void * __cdecl operator new(

_in_ size_t,

_in_ _ty* p)

template void __cdecl operator delete(

_in_ void* /* pv */,

_in_ _ty* /* p */)

t t;

};// implementation

void internalsetatindex(

_in_ int nindex,

_in_ const t& t)

typedef t _arrayelementtype;

t* m_at;

int m_nsize;

int m_nallocsize;

};#define c******valarray c******array

template inline c******array::~c******array()

以上就是我們所要說的c******array類 的主要**。我之所以把這個類作為典範,是因為它足夠簡單,而且充分展現了模板類的魅力。你可以從它的**裡學習c++的模板類語法,同時你還可以學習模板成員變數以及模板函式的寫法兒。實際上我經常在我的程式裡使用這個類,如果是簡單的結構或者簡單型別(int,long,float,double,char,byte)的陣列,我幾乎第乙個想到的就是這個集合類,它們工作的很好,從沒有讓我失望。

下面是乙個示例:

struct teststru

; c******arraytestary;

teststru ts;

ts.m_order = 1;

lstrcpy( ts.m_name, _t("測試a"));

testary.add(ts);

ts.m_order = 2;

lstrcpy( ts.m_name, _t("測試b"));

testary.add(ts);

ts.m_order = 3;

lstrcpy( ts.m_name, _t("測試c"));

testary.add(ts);

你這麼使用時沒有問題的,這個teststru換做其他型別,比如float, 工作的只會更好。c******array 類在我們已知陣列大小的情況下效率有些欠缺。因為它缺少乙個一次性初始化陣列記憶體的函式。但如果你不能確定陣列大小,那麼它很合適,而且效率不低。

c******array類,你可以複製乙個陣列, 你還可以刪除指定位置的陣列項,或者用下標方式給陣列的項賦新值。

c******arraycopyary(testary); 

ts.m_order = 3;

lstrcpy( ts.m_name, _t("將被刪除"));

testary[2] = ts;

testary.removeat(2);

你還可以查詢乙個項,或者刪除乙個指定的項。當然這涉及到判斷相等的操作,這需要一點前提。 對於int,float型別,編譯器知道他們的相等操作,如果是你自己定義的結構,那麼編譯器是不知道的。

c******array 類好就好在,如果你沒有用到這個操作, 前面講的就夠了,編譯器也不會報錯。如果你用到了相等操作,比如查詢,比如刪除。那麼編譯器會要求你為你的結構提供==操作符。

int nfind = testary.find(ts);

testary.remove(ts);

增加上面兩行**編譯器會報錯。提示沒有 == 操作符,所以我們修改teststruct 的**,新增自定義操作符 == ,如下:

struct teststru

};

這麼一來,編譯可以通過了。如果兩個結構的m_name欄位的文字相同,就認為他們相等。

到這裡這個c******array類就介紹完了。需要補充一點的就是, c******array類 也可以處理指標,你可以把任何乙個類的指標作為模板類, 建立陣列。例如:

c******arrayptrarray; 這樣用也沒有問題,這裡是指標的話,那麼最後就涉及指標的申請和釋放問題,處理好也沒什麼問題。

最後一點,額外囉嗦一下,下面這個包裝類:

template void *operator new(

_in_ size_t,

_in_ _ty* p)

template void operator delete(

_in_ void* /* pv */,

_in_ _ty* /* p */)

t t;

};

類模板的練習 陣列類模板

array.h ifndef array h define array h include using namespace std template class array public array int len 0 構造 this len len m p new t len array cons...

簡單類模板

include include struct student 結構體student template 類模板 實現對任意型別資料進行訪問 class store template store store template t store getelem int main stores1,s2 sto...

陣列類模板

問題及描述 檔名稱 test.cpp 作者 邱凱 完成日期 2016年5月31號 版本號 v6.0 問題描述 陣列類模板 輸入描述 輸入資料 輸出描述 輸出答案 include include include using namespace std template class array temp...