溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何使用c++模板自定義數(shù)組

發(fā)布時(shí)間:2022-03-21 13:33:16 來(lái)源:億速云 閱讀:166 作者:小新 欄目:開發(fā)技術(shù)

這篇文章主要介紹了如何使用c++模板自定義數(shù)組,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。


前言:

制造通用模板,創(chuàng)建自定義的數(shù)組,

一個(gè)數(shù)組,里面有這么幾個(gè)屬性,數(shù)組容量,數(shù)組元素個(gè)數(shù),數(shù)組本身內(nèi)存地址,這幾個(gè)數(shù)據(jù)都是定義私有類型,提供有參構(gòu)造,讓用戶可以構(gòu)造出這個(gè)數(shù)組對(duì)象。下面是有參構(gòu)造和拷貝構(gòu)造和析構(gòu)函數(shù)還有operator=重載的代碼

在前面類模板中成員函數(shù)創(chuàng)建有這個(gè)主意問題,最好的辦法就是把類模板寫在一個(gè)hpp的文件中,不要拆開寫成多個(gè)文件

1.自定義數(shù)組.hpp--文件

#pragma once
#include<iostream>
using namespace std;
#include<string>
template<class T>
class Myarry
{
public:
  Myarry() {};//自己創(chuàng)建有參構(gòu)造,編譯器就不提供無(wú)參構(gòu)造,所以必須自己寫一次無(wú)參構(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ù)組必須加[],否則會(huì)引發(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ù)拷貝過(guò)來(lái)
    {
      this->marry[i] = arr->marry[i];
    }
  }
  //等號(hào)賦值
  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)//通過(guò)下標(biāo)訪問數(shù)組,并使它作為左值加&
  {
    if (index > this->capacity)
    {
      cout << "訪問越界!" << endl;
      exit(0);
    }
    return this->marry[index];
  }
  int gercapacity()//獲取數(shù)組容量
  {
    return this->capacity;
  }
  int getsize()//獲取數(shù)組元素個(gè)數(shù)
  {
    return this->size;
  }
private:
  T * marry;//數(shù)組
  int capacity;//數(shù)組容量
  int size;//數(shù)組元素個(gè)數(shù)
};

2.測(cè)試文件

#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;
}

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“如何使用c++模板自定義數(shù)組”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來(lái)學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

c++
AI