当前位置:网站首页>STL教程2-MyArray框架实现
STL教程2-MyArray框架实现
2022-06-21 08:23:00 【贪睡的蜗牛】
能够存放任何类型数据,实现初始化,=符号重载,右值引用,内存释放
1、基础框架
1、类成员变量
容量 int mCapacity;
现有元素 int mSize;
数据的首地址int *pAddr;
2、类函数
MyArray(int capacity)//实现array初始化
MyArray()//实现类初始化
T& operator[](int index)//返回数组指定元素 比如arr[3]
MyArray<T> operator=(const MyArray<T>& arr)//MyArray<int> arr2(10); arr2 = arr;
void PushBack(T& data)// 往里面添加元素,这个方法只能添加左值 a=3 arr.PushBack(a);
void PushBack(T&& data) // 往里面添加右值 ,这个方法能够实现arr.PushBack(3);
~MyArray(); //析构函数,释放类的空间
在class上面加入template< class T>
2、MyArray(int capacity)
MyArray(int capacity)
{
this->mCapacity = capacity;
this->mSize = 0;
//申请内存
this->pAddr = new T[this->mCapacity];
}
3、MyArray(const MyArray& arr)
MyArray(const MyArray<T>& arr) {
this->mCapacity = arr.mCapacity;
this->mSize = arr.mSize;
//申请内存
this->pAddr = new T[this->mCapacity];
for (int i = 0; i < mSize; i++) {
pAddr[i] = arr.pAddr[i];
}
}
4、T& operator[](int index)
T& operator[](int index) {
//pAddr是一个T类型的数组,这里返回的是在数组中指定index位置的元素
return this->pAddr[index];
}
5、MyArray operator=(const MyArray& arr)
MyArray<T> operator=(const MyArray<T>& arr) {
//如果已经申请了空间,那么就释放空间
if (this->pAddr != NULL) {
delete[]pAddr;
}
this->mCapacity = arr.mCapacity;
this->mSize = arr.mSize;
//申请内存
this->pAddr = new T[this->mCapacity];
for (int i = 0; i < mSize; i++) {
pAddr[i] = arr.pAddr[i];
}
//返回class的指针
return *this;
}
6、void PushBack(T&& data)
void PushBack(T&& data) {
if (mSize >= mCapacity)return 0;
//注意msize是从1开始的
pAddr[mSize] = data;
mSize++;
}
测试
MyArray<int> arr(10);
int a = 10, b = 20;
arr.PushBack(a);
arr.PushBack(b);
完整代码
#include <iostream>
using namespace std;
template<class T>
class MyArray {
public:
//容量
int mCapacity;
//现有元素
int mSize;
//数据的首地址
int *pAddr;
public :
MyArray(int capacity)
{
this->mCapacity = capacity;
this->mSize = 0;
//申请内存
this->pAddr = new T[this->mCapacity];
}
MyArray(const MyArray<T>& arr) {
this->mCapacity = arr.mCapacity;
this->mSize = arr.mSize;
//申请内存
this->pAddr = new T[this->mCapacity];
for (int i = 0; i < mSize; i++) {
pAddr[i] = arr.pAddr[i];
}
}
T& operator[](int index) {
//pAddr是一个T类型的数组,这里返回的是在数组中指定index位置的元素
return this->pAddr[index];
}
//=符号重载
MyArray<T> operator=(const MyArray<T>& arr) {
//如果已经申请了空间,那么就释放空间
if (this->pAddr != NULL) {
delete[]pAddr;
}
this->mCapacity = arr.mCapacity;
this->mSize = arr.mSize;
//申请内存
this->pAddr = new T[this->mCapacity];
for (int i = 0; i < mSize; i++) {
pAddr[i] = arr.pAddr[i];
}
//返回class的指针
return *this;
}
void PushBack(T& data) {
if (mSize >= mCapacity)return ;
//注意msize是从1开始的
//1、对象元素必须能够拷贝
//2、容器都是值语义,而非引用语义,向容器中放入元素都是放入元素的拷贝份,也就是都是放的拷贝
//3、如果元素的成员有指针,注意深拷贝和浅拷贝
//3.1 深拷贝和浅拷贝
this->pAddr[this->mSize] = data;
this->mSize++;
}
~MyArray() {
if (this->pAddr != NULL) {
delete[] this->pAddr;
}
}
//对右值取引用
void PushBack(T&& data) {
if (mSize >= mCapacity)return 0;
//注意msize是从1开始的
pAddr[mSize] = data;
mSize++;
}
};
void test01()
{
MyArray<int> arr(10);
int a = 10, b = 20;
arr.PushBack(a);
arr.PushBack(b);
// 不能arr.PushBack(2);不能对右值取引用
/* 左值可以在多行使用比如a=10; * 右值仅是临时变量,只能在当前行使用 */
}
class Person {
};
void test02() {
Person p1, p2;
MyArray<Person> arr2(10);
arr2.PushBack(p1);
arr2.PushBack(p2);
}
int main() {
test01();
return 0;
}
7、左值和右值
int a = 10, b = 20;
arr.PushBack(a);
arr.PushBack(b);
void PushBack(T& data) {
if (mSize >= mCapacity)return ;
//注意msize是从1开始的
//1、对象元素必须能够拷贝
//2、容器都是值语义,而非引用语义,向容器中放入元素都是放入元素的拷贝份,也就是都是放的拷贝
//3、如果元素的成员有指针,注意深拷贝和浅拷贝
//3.1 深拷贝和浅拷贝
this->pAddr[this->mSize] = data;
this->mSize++;
}
上述代码只能传入变量即只能传入一个左值,右值的话仅在当前行生效,因此如果想要传入右值,需要看c++11新引入的
void PushBack(T&& data) {
if (mSize >= mCapacity)return 0;
//注意msize是从1开始的
pAddr[mSize] = data;
mSize++;
}
8、容器都是值语义
容器都是值语义,而非引用语义,向容器中放入元素都是放入元素的拷贝份,也就是都是放的拷贝
如果元素的成员有指针,注意深拷贝和浅拷贝
9、深拷贝和浅拷贝
如果有指针,那么指针指向的地址是否是拷贝
边栏推荐
- WordPress media library supports uploading and previewing SVG icons
- 5 minutes to understand MySQL - row to column
- Unity development related blog collection
- 2022-2028 global cooling on-off valve industry research and trend analysis report
- Complex four operations (23 lines of concise code)
- showCTF Web入门题系列
- Unity写多线程注意事项
- Redis master-slave vulnerability and remote connection vulnerability
- Antd table how scroll bars appear in long tables
- Post-Process初级使用笔记(重要的几项)
猜你喜欢

2022-2028 global hydrogen internal combustion engine industry research and trend analysis report

Windows10 LAN shared folder process

2022-2028 global hydrogen engine industry research and trend analysis report

请问这些要求用mysql能实现吗

The market value of Jinshan office fell below 100 billion yuan: the shareholder Qiwen n-dimensional cash out exceeded 1.2 billion yuan

Two image enhancement methods: image point operation and image graying

Visual studio code annotation plug-in: korofileheader

tidb4.0.0遇见的问题、报错总结(tiup部署)

Eureka的TimedSupervisorTask类(自动调节间隔的周期性任务)

【kotlin】第一天
随机推荐
4.4 Eval function replaces function
GQL+Nodejs+MySQL数据库
移动应用开发总结
Showctf web primer series
Can you implement these requirements with MySQL
Unity写多线程注意事项
Dumpling备份数据库
日记(C语言总结)
Using the method of combining shapes in illustrator
Visual studio code annotation plug-in: korofileheader
Classic topics of leetcode array (I)
Unity write multithreading considerations
WordPress media library supports uploading and previewing SVG icons
Journal (résumé en langue c)
Multiplication and addition of univariate polynomial (20 points)
For hand drawn graphics, covering multiple topics, CVPR 2022 sketchdl workshop begins to solicit contributions!
Detailed introduction to PHP classes and objects
Linux Installation of Damon database /dm8 (with client tool installation full version)
4.6 lodash usage documents
Blue Bridge Cup: Candy