c++模板自定義數(shù)組
前言:
制造通用模板,創(chuàng)建自定義的數(shù)組,
一個數(shù)組,里面有這么幾個屬性,數(shù)組容量,數(shù)組元素個數(shù),數(shù)組本身內(nèi)存地址,這幾個數(shù)據(jù)都是定義私有類型,提供有參構(gòu)造,讓用戶可以構(gòu)造出這個數(shù)組對象。下面是有參構(gòu)造和拷貝構(gòu)造和析構(gòu)函數(shù)還有operator=重載的代碼
在前面類模板中成員函數(shù)創(chuàng)建有這個主意問題,最好的辦法就是把類模板寫在一個hpp的文件中,不要拆開寫成多個文件
1.自定義數(shù)組.hpp--文件
#pragma once #include<iostream> using namespace std; #include<string> template<class T> class Myarry { public: ? Myarry() {};//自己創(chuàng)建有參構(gòu)造,編譯器就不提供無參構(gòu)造,所以必須自己寫一次無參構(gòu)造,即使是空實(shí)現(xiàn)也要寫! ? Myarry(int capacity)//有參構(gòu)造函數(shù) ? { ? ? this->capacity = capacity; ? ? this->size = 0; ? ? this->marry = new T[this->capacity];//把數(shù)組創(chuàng)建在堆區(qū) ? } ? ~Myarry()//析構(gòu)函數(shù) ? { ? ? if (this->marry !=NULL) ? ? { ? ? ? delete []this->marry;//析構(gòu)數(shù)組必須加[],否則會引發(fā)斷點(diǎn) ? ? ? marry = NULL; ? ? ? this->capacity = 0; ? ? ? this->size = 0; ? ? } ? } ? Myarry(const Myarry& arr)//拷貝構(gòu)造 ? { ? ? this->capacity = arr.capacity; ? ? this->size = arr.size; ? ? this->marry = new T[arr.capacity]; ? ? for (int i = 0; i < arr.size; i++)//把數(shù)據(jù)拷貝過來 ? ? { ? ? ? this->marry[i] = arr->marry[i]; ? ? } ? } ? //等號賦值 ? Myarry& operator=(const Myarry& arr) ? { ? ? if (this->marry != NULL)//如果有數(shù)據(jù)先清空,再賦值 ? ? { ? ? ? delete[]this->marry; ? ? ? this->marry ?= NULL; ? ? ? this->size = 0; ? ? ? this->capacity = 0; ? ? } ? ? this->capacity = arr.capacity; ? ? this->size = arr.size; ? ? this->marry = new T[this->capacity]; ? ? for (int i = 0; i < this->size; i++)//將數(shù)據(jù)進(jìn)行拷貝 ? ? { ? ? ? this->marry[i] = arr.marry[i]; ? ? } ? ? return *this; ? } ? void pushback(const T&ptr)//尾加法 ? { ? ? if (this->capacity == this->size) ? ? { ? ? ? cout << "容量已滿!" << endl; ? ? ? return; ? ? } ? ? this->marry[this->size] = ptr; ? ? this->size++; ? } ? void deleteback()//尾刪法 ? { ? ? if (this->size == 0) ? ? { ? ? ? cout << "數(shù)據(jù)為零,沒有可刪數(shù)據(jù)!" << endl; ? ? } ? ? delete this->marry[this->size - 1]; ? ? this->size--; ? } ? T & operator[](int index)//通過下標(biāo)訪問數(shù)組,并使它作為左值加& ? { ? ? if (index > this->capacity) ? ? { ? ? ? cout << "訪問越界!" << endl; ? ? ? exit(0); ? ? } ? ? return this->marry[index]; ? } ? int gercapacity()//獲取數(shù)組容量 ? { ? ? return this->capacity; ? } ? int getsize()//獲取數(shù)組元素個數(shù) ? { ? ? return this->size; ? } private: ? T * marry;//數(shù)組 ? int capacity;//數(shù)組容量 ? int size;//數(shù)組元素個數(shù) };
2.測試文件
#include "自定義數(shù)組.hpp" class person { public: ? person() ? { ? ? this->age = 0; ? } ? int age; ? string name; }; void text01() { ? person p[4]; ? p[0].age = 20; ? p[0].name = "張三"; ? p[1].age = 0; ? p[1].name = "李四"; ? p[2].age = 40; ? p[2].name = "王五"; ? p[3].age = 80; ? p[3].name = "趙六"; ? Myarry<person>pp(10); ? for (int i = 0; i < 4; i++) ? { ? ? pp.pushback(p[i]); ? } ? for (int i = 0; i < pp.getsize(); i++) ? { ? ? cout << pp[i].name<<pp[i].age<< endl; ? } } void text02() { ? Myarry<int>inta(10); ? for (int i = 0; i < 5; i++) ? { ? ? inta.pushback(i); ? } ? for (int i = 0; i < inta.getsize(); i++) ? { ? ? cout << inta[i] << endl; ? } } int main() { ? /*text02();*/ ? text01(); ? return 0; }
到此這篇關(guān)于c++模板自定義數(shù)組的文章就介紹到這了,更多相關(guān)c++自定義數(shù)組內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
string中c_str(),data(),copy(p,n)函數(shù)的用法總結(jié)
以下是對string中c_str(),data(),copy(p,n)函數(shù)的用法進(jìn)行了詳細(xì)的介紹,需要的朋友可以過來參考下2013-09-09C語言實(shí)現(xiàn)linux網(wǎng)卡檢測改進(jìn)版
這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)linux網(wǎng)卡檢測的改進(jìn)版,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-06-06C語言入門篇--sizeof與strlen基礎(chǔ)理論
本篇文章是c語言基礎(chǔ)篇,主要為大家介紹了C語言的sizeof與strlen的基本理論知識,希望可以幫助大家快速入門c語言的世界,更好的理解c語言2021-08-08Qt中QList與QLinkedList類的常用方法總結(jié)
這篇文章主要為大家詳細(xì)介紹了Qt中QList與QLinkedList類的常用方法,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)Qt有一定的幫助,需要的可以參考一下2022-12-12