首页 > 其他分享 >类模板实现简单的数组

类模板实现简单的数组

时间:2024-01-13 21:22:56浏览次数:20  
标签:arr int MyArray msize 数组 简单 mCapacity Maker 模板

//Myarray.hpp
#pragma once

template<class T>

class MyArray {
public:
	MyArray(int capacity) {
		this->mCapacity = capacity;
		this->msize = 0;
		this->p = new T[this->mCapacity];
	}
	//copy
	MyArray(const MyArray& arr) {
		this->mCapacity = arr.mCapacity;
		this->msize = arr.msize;
		p = new T[arr.mCapacity];
		for (int i = 0; i < this->msize; ++i) p[i] = arr.p[i];
		
	}

	//operator =
	MyArray& operator=(const MyArray& arr){
		if (this->p != nullptr) {
			delete[] this->p;
			this->p = nullptr;
		}
		p = new T[arr.mCapacity];
		this->msize = arr.msize;
		this->mCapacity = arr.mCapacity;
		for (int i = 0; i < this->msize; ++i) p[i] = arr.p[i];
		return *this;
	}

	void push_back(const T& val) {
		if (this->msize == this->mCapacity) {
			T* np = new T[2 * this->mCapacity]{};
			this->mCapacity *= 2;
			for (int i = 0; i < this->msize; ++i) np[i] = this->p[i];
			delete[] this->p;
			this->p = nullptr;
			this->p = np;
		}
		this->p[this->msize] = val;
		++this->msize;
	}

	void pop_back() {
		if (this->msize == 0) return;
		this->p[msize] = {};
		--this->msize;
	}

	T operator[](int i) {
		return this->p[i];
	}

	~MyArray() {
		if (this->p != nullptr) {
			delete[] p;
			p = nullptr;
		}
	}

	int size() {
		return this->msize;
	}

private:
	T* p;
	int mCapacity;
	int msize;
};

这里是头文件

//main.cpp
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<algorithm>
#include<numeric>
#include"Myarray.hpp"
using i64 = long long;

class Maker {
public:
	Maker() {}
	Maker(std::string name,int age){
		this->name = name;
		this->age = age;
	}
	std::string name;
	int age;
};

void printMaker(MyArray<Maker>& arr) {
	for (int i = 0; i < arr.size(); ++i) std::cout << arr[i].name << ' ' << arr[i].age << '\n';
}

void test() {
	MyArray<Maker> m(1);
	Maker m1("123", 200);
	Maker m2("321", 1200);
	Maker m3("456", 2100);
	Maker m4("654", 11200);
	m.push_back(m1);
	m.push_back(m2);
	m.push_back(m3);
	m.push_back(m4);
	printMaker(m);
}

auto main()->int32_t {

	std::cin.tie(nullptr)->sync_with_stdio(false);
	
	test();

	
	return static_cast<int32_t>(0);
}

标签:arr,int,MyArray,msize,数组,简单,mCapacity,Maker,模板
From: https://www.cnblogs.com/lambdaios/p/17962961

相关文章

  • 类模板和友元
    友元内部实现#include<iostream>#include<stdio.h>#include<stdlib.h>#include<algorithm>#include<numeric>template<classNameType,classAgeType>classMark{ friendvoidprintMaker(Mark<NameType,AgeType>&p)......
  • 数组内存图
    ......
  • 数组
    ......
  • 2024-01-03:用go语言,给你两个长度为 n 下标从 0 开始的整数数组 cost 和 time, 分别表示
    2024-01-03:用go语言,给你两个长度为n下标从0开始的整数数组cost和time,分别表示给n堵不同的墙刷油漆需要的开销和时间。你有两名油漆匠,一位需要付费的油漆匠,刷第i堵墙需要花费time[i]单位的时间,开销为cost[i]单位的钱。一位免费的油漆匠,刷任意一堵墙的时间为1......
  • python 2数组同时排序
    Python2数组同时排序在Python中,排序是一种常见的操作。当我们想要对多个数组进行排序时,可以使用zip()函数和sorted()函数来实现。本文将详细介绍如何使用这两个函数对数组进行排序,并提供相应的代码示例。首先,我们需要了解zip()函数的作用。zip()函数可以将多个数组中的元素一一......
  • 简单说一下mybatis的多表映射的使用
    MyBatis是一种流行的Java持久层框架,它提供了一种简单而强大的方式来进行数据库操作。在MyBatis中,多表映射是指将数据库中多个关联表之间的数据映射到Java对象中的过程。MyBatis提供了多种方式来实现多表映射,下面是其中两种常用的方法:使用嵌套查询(NestedQueries):这是一......
  • Jenkins邮件模板
    模板一:1<!DOCTYPEhtml>2<html>3<head>4<metacharset="UTF-8">5<title>${ENV,var="JOB_NAME"}-第${BUILD_NUMBER}次构建日志</title>6</head>78<bodyleftmargin="8"marginw......
  • 【opencv学习笔记】028之模板匹配——matchTemplate函数详解
    目录​ ​一、前言​​​ ​二、模板匹配​​​ ​1、模板匹配是个啥​​​ ​2、常用匹配算法​​​​ ​3、API​​​ ​4、代码展示​​​ ​5、执行结果​​一、前言遭遇了点突发情况,所以今天更新的有点晚,也不知道能不能等到今天发出去了。终于可以从模板匹......
  • Simple CTF-简单CTF
    首先对靶机进行端口扫描nmap-sV-p-10.10.70.110通过扫描结果可以看出靶机开放了21端口vsftpd服务、80端口apache服务、2222端口openssh服务这里我们的思路是匿名用户登录ftp,获取一些有用的文件80端口目录扫描,robots文件,指纹识别ssh爆破经过我的尝试ftp无法正常执行......
  • Microsoft 365 新功能速递:通信合规性–检测Copilot for Microsoft 365交互模板
    51CTO博客链接:https://blog.51cto.com/u_13637423即将推出的MicrosoftPurviewCommunicationCompliance是一个新模板,专门用于分析所有CopilotforMicrosoft365提示和响应,预计在2024年1月底正式发布。CommunicationCompliance正在引入一个新模板,专门用于分析所有CopilotforMi......