当前位置: 首页 > news >正文

C++案例 自定义数组

#include <string>
#include <iostream>
#include <algorithm>
#include <initializer_list>
using namespace std;template<class T>
class myArray
{// 类模板 友元 重载 外部类实现// cout << array << endl;friend ostream& operator<< <T>(ostream& cout, const myArray<T>& array);
public:myArray(int size) {this->m_size = size;pAddress = new T[size]; }myArray(const myArray<T>& array) {this->m_size = array.m_size;this->pAddress = new T[array.m_size]; copy(array.pAddress, array.pAddress + array.m_size, this->pAddress);}~myArray() {if (pAddress != nullptr) {delete[] pAddress;  pAddress = nullptr;}}// array = {1,2,3,4,5}myArray& operator=(initializer_list<T> list) {if (this->pAddress != nullptr) {delete[] this->pAddress;this->pAddress = nullptr;}this->m_size = list.size();this->pAddress = new T[m_size];copy(list.begin(), list.end(), this->pAddress);return *this;}// array1 = array2myArray& operator=(const myArray<T>& array) {if (this != &array) {  // 自赋值检查if (this->pAddress != nullptr) {delete[] this->pAddress;  this->pAddress = nullptr;}this->m_size = array.m_size;this->pAddress = new T[array.m_size]; copy(array.pAddress, array.pAddress + array.m_size, this->pAddress);}return *this;}// array = (T *)arrmyArray& operator=(const T* array) {for (int i = 0; i < m_size; i++) {pAddress[i] = array[i];}return *this;}// array[0]T &operator[](int num) {return this->pAddress[num];}// 尾插法// 尾删法private:T* pAddress;int m_size;
};template<class T>
ostream& operator<<(ostream& cout, const myArray<T>& array) {cout << "[";for (int i = 0; i < array.m_size; i++) {cout << array.pAddress[i];if (i != array.m_size - 1) cout << ", ";  }cout << "]";return cout;
}

  

http://www.hskmm.com/?act=detail&tid=37654

相关文章:

  • 【周记】2025.10.13~2025.10.19
  • 背包
  • 10.23《程序员修炼之道 从小工到专家》第二章 注重实效的途径 - GENGAR
  • 玩转单片机之智能车小露——LED闪烁实战
  • ord() 函数
  • 2025.10.23总结 - A
  • 大模型 | VLA 初识及在自动驾驶场景中的应用
  • ExPRT.AI如何预测下一个将被利用的漏洞
  • Redis中的分布式锁之SETNX底层实现
  • 攻击模拟
  • 2025家纺摄影公司推荐,南通鼎尚摄影专注产品视觉呈现
  • AI元人文构想的跨学科研究:技术实现与人文影响分析——对自由与责任的再框架化(DeepSeek基于Ai元人文系列文章研究)
  • Python---简易编程解决工作问题
  • 日总结 16
  • 比赛题解 总结
  • DM8 安装包 for linux_x86
  • MPK(Mirage Persistent Kernel)源码笔记(1)--- 基础原理
  • 模拟can通信
  • 解题报告-拯救计划(概率 DP)
  • 日志分析-IIS日志分析
  • Min_25 筛
  • 解码Linux文件IO之库的制作与应用
  • 20251023 正睿二十连测
  • 1019:浮点数向零舍入(分正负取整)
  • 二分图/忆re.
  • 《IDEA 2025长效采用配置指南:有效期配置至2099年实战之JetBrains全家桶有效》​
  • ZKW线段树
  •  pytorch 66页实验题
  • Visual Studio 插件 - 喝水提醒 - 指南
  • JAVA 排序用法