MyArray類模板實現

2021-08-18 04:26:31 字數 1027 閱讀 6994

#define _crt_secure_no_warnings

#include #include using namespace std;

template class myarray

myarray(const myarray&arr) }

~myarray() }

t &operator (int index)

myarrayoperator = (const myarray&arr)

this->mcapacity = arr.mcapacity;

this->msize = arr.msize;

this->paddr = new t[this->mcapacity];

for (int i = 0; i < this->msize; i++)

return *this;

} void pushback(t &data)

//呼叫拷貝構造 =號操作符

//1.物件元素必須能夠被拷貝

//2.容器都是值寓意的,而非引用寓意的,向容器中放入元素,都是放入元素的拷貝份

//3.如果元素的成員有指標,注意深拷貝和淺拷貝問題

this->paddr[this->msize] = data;

//msize++

this->msize ++;

} void pushback(t &&data) //對右值進行取引用 c++11新特性

this->paddr[this->msize] = data;

//msize++

this->msize++;

}public:

//一共可以容納多少個元素

int mcapacity;

//當前陣列有多少元素

int msize;

//儲存資料的首位址

t* paddr;

};void test()

cout << endl;

}int main()

類模板 MyArray框架搭建和函式實現

include using namespace std template class myarray myarray myarray const myarray arr 拷貝建構函式,使用const,防止傳入引數改變 t operator int index myarrayoperator cons...

類模板實現單鏈表

特別注意 編寫函式前必須先進行模板的宣告 includeusing namespace std templateclass list 宣告list類 templateclass listnode listnode type d,listnode n null data d next n listno...

c 實現類模板實現堆排序

1 首先你要會堆排序 可以點這裡看一下堆排序過程 2.使用類模板實現堆排序 include template class t 堆排序 void heapsort t t,int len template class t 調整堆 從上到下調整堆 void adjustheap t t,int poin...