当前位置: 首页 > 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.jsqmd.com/news/20730/

相关文章:

  • 20251023周四日记
  • 10.23《程序员修炼之道 从小工到专家》第二章 注重实效的途径 - GENGAR
  • 玩转单片机之智能车小露——LED闪烁实战
  • ord() 函数
  • 2025.10.23总结 - A
  • 树状数组求逆序对
  • 大模型 | VLA 初识及在自动驾驶场景中的应用
  • Redis中的分布式锁之SETNX底层实现
  • 2025家纺摄影公司推荐,南通鼎尚摄影专注产品视觉呈现
  • ExPRT.AI如何预测下一个将被利用的漏洞
  • 求函数
  • AI元人文构想的跨学科研究:技术实现与人文影响分析——对自由与责任的再框架化(DeepSeek基于Ai元人文系列文章研究)
  • Python---简易编程解决工作问题
  • DM8 安装包 for linux_x86
  • 日总结 16
  • MPK(Mirage Persistent Kernel)源码笔记(1)--- 基础原理
  • 背包dp(1)
  • 模拟can通信
  • 202501软件工程第二次团队作业
  • 题解:P14174 【MX-X23-T4】卡常数
  • 比赛题解 总结
  • 解题报告-拯救计划(概率 DP)
  • 解码Linux文件IO之库的制作与应用
  • 20251023 正睿二十连测
  • 1019:浮点数向零舍入(分正负取整)
  • 创建 SQL Server 数据库【通用】
  • HNSW算法实战:用分层图索引替换k-NN暴力搜索
  • 日志分析-IIS日志分析
  • Spring Boot 自动配置之 TaskExecutor - 实践
  • 二分图/忆re.