当前位置: 首页 > 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.gsyq.cn/news/28739.html

相关文章:

  • 20251023周四日记
  • ord() 函数
  • Redis中的分布式锁之SETNX底层实现
  • 2025家纺摄影公司推荐,南通鼎尚摄影专注产品视觉呈现
  • 求函数
  • Python---简易编程解决工作问题
  • MPK(Mirage Persistent Kernel)源码笔记(1)--- 基础原理
  • 背包dp(1)
  • 比赛题解 总结
  • 1019:浮点数向零舍入(分正负取整)
  • 创建 SQL Server 数据库【通用】
  • HNSW算法实战:用分层图索引替换k-NN暴力搜索
  • Spring Boot 自动配置之 TaskExecutor - 实践
  • 二分图/忆re.
  • 《IDEA 2025长效采用配置指南:有效期配置至2099年实战之JetBrains全家桶有效》​
  • 如何制作PDF文件目录? - 详解
  • todesk远程到被控Mac后能看到画面,鼠标键盘执行无反应
  • JAVA 排序用法
  • esp32-usb-jtag 调试踩坑
  • MySQLDay3
  • 飞牛OS通过docker部署SillyTavern酒馆
  • 深入解析:XML中的 CDATA mybaitis xml中的 <![CDATA[ xxxx ]]>
  • AI股票预测分析报告 - 2025年10月23日 20:26
  • 软件包管理
  • .NET Core报错克服【无废话上操作】
  • 题解:P11831 [省选联考 2025] 追忆
  • 2025-10-23 MX-S 模拟赛 赛后总结【MX】
  • Anaconda命令大全conda
  • 网络设备
  • Kafka-保证消息消费的顺序性及高可用机制 - 教程