欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

c++模板自定義數(shù)組

 更新時間:2022年03月21日 11:39:27   作者:無痕戀雪  
這篇文章主要介紹了c++模板自定義數(shù)組,通過制造通用模板,創(chuàng)建自定義的數(shù)組展開文章相關(guān)內(nèi)容,具有一的參考價值,需要的小伙伴可以參考一下

前言:

制造通用模板,創(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ù)的用法總結(jié)

    以下是對string中c_str(),data(),copy(p,n)函數(shù)的用法進(jìn)行了詳細(xì)的介紹,需要的朋友可以過來參考下
    2013-09-09
  • 詳解C++ sizeof(上)

    詳解C++ sizeof(上)

    這篇文章主要介紹了C++ sizeof的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)c++,感興趣的朋友可以了解下
    2020-08-08
  • EasyC++模板重載

    EasyC++模板重載

    這篇文章主要介紹了C++模板重載,重載的模板的函數(shù)特征,也就是入?yún)⒌臄?shù)量和類型必須有所不同,下面我們講舉例說明此內(nèi)容,具有一定的參考價值,需要的小伙伴可以參考一下
    2021-12-12
  • C語言實(shí)現(xiàn)linux網(wǎng)卡檢測改進(jìn)版

    C語言實(shí)現(xiàn)linux網(wǎng)卡檢測改進(jìn)版

    這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)linux網(wǎng)卡檢測的改進(jìn)版,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-06-06
  • c語言實(shí)現(xiàn)一個簡單日歷

    c語言實(shí)現(xiàn)一個簡單日歷

    本文給大家分享的是一則使用C語言來實(shí)現(xiàn)的一個簡單日歷的代碼,根據(jù)項(xiàng)目需求,實(shí)現(xiàn)了3個簡單的小功能,推薦給大家,有需要的小伙伴可以參考下。
    2015-03-03
  • 用C++來解決3*3拼圖的問題

    用C++來解決3*3拼圖的問題

    這篇文章主要介紹了用C++來解決3*3拼圖的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • C++模板 index_sequence使用示例詳解

    C++模板 index_sequence使用示例詳解

    這篇文章主要為大家介紹了C++模板 index_sequence使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-12-12
  • C語言入門篇--sizeof與strlen基礎(chǔ)理論

    C語言入門篇--sizeof與strlen基礎(chǔ)理論

    本篇文章是c語言基礎(chǔ)篇,主要為大家介紹了C語言的sizeof與strlen的基本理論知識,希望可以幫助大家快速入門c語言的世界,更好的理解c語言
    2021-08-08
  • C++模板之特化與偏特化詳解

    C++模板之特化與偏特化詳解

    這篇文章主要介紹了C++模板之特化與偏特化詳解,本文講解了什么是C++模板、模板特化、模板偏特化、特化與偏特化的調(diào)用順序等內(nèi)容,需要的朋友可以參考下
    2014-10-10
  • Qt中QList與QLinkedList類的常用方法總結(jié)

    Qt中QList與QLinkedList類的常用方法總結(jié)

    這篇文章主要為大家詳細(xì)介紹了Qt中QList與QLinkedList類的常用方法,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)Qt有一定的幫助,需要的可以參考一下
    2022-12-12

最新評論