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)造,即使是空實現(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ā)斷點
? ? ? 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)文章
C語言從txt文件中逐行讀入數(shù)據(jù)存到數(shù)組中的實現(xiàn)方法
下面小編就為大家?guī)硪黄狢語言從txt文件中逐行讀入數(shù)據(jù)存到數(shù)組中的實現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-12-12
OpenCV中的cv::Mat函數(shù)將數(shù)據(jù)寫入txt文件
這篇文章主要介紹了OpenCVcv::Mat中的數(shù)據(jù)按行列寫入txt文件中,需要的朋友可以參考下2018-05-05
C語言執(zhí)行時,程序控制臺輸出窗口 一閃而過問題及解決
這篇文章主要介紹了C語言執(zhí)行時,程序控制臺輸出窗口 一閃而過問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-11-11

