当前位置:网站首页>小实操:实现Myarray
小实操:实现Myarray
2022-07-21 05:16:00 【原来只是一阵风】
首先放重点:
容器都是值寓意,而非引用寓意。向容器中放入元素,都是放入元素的拷贝份(上黑马程序员网课的老师处得知)
#include<iostream>
using namespace std;
template<class T>
class MyArray
{
public:
MyArray(int capacity) //构造数组
{
this->mCapacity = capacity;
this->mSize = 0; //数组中不存在元素
//分配内存
this->ptr = new T[this->mCapacity];
}
MyArray(const MyArray& arr) //拷贝构造
{
this->mCapacity = arr.mCapacity;
this->mSize = arr.mSize;
this->ptr = new T[this->mCapacity];
//拷贝数据
for (int i = 0; i < this->mSize; ++i)
{
this->ptr[i] = arr.ptr[i];
}
}
MyArray<T> operator=(const MyArray& arr) //重载等号
{
if (this->ptr != NULL) //数组中有元素就删除
{
delete[] this->ptr;
}
this->mCapacity = arr.mCapacity;
this->mSize = arr.mSize;
this->ptr = new T[this->mCapacity];
//拷贝数据
for (int i = 0; i < this->mSize; ++i)
{
this->ptr[i] = arr.ptr[i];
}
return *this;
}
T& operator[](int index) //数组下标
{
return this->ptr[index];
}
void Pushback(T& data) //插入数组
{
if (this->mSize >= this->mCapacity)
{
cout << "数组已满" << endl;
return;
}
this->ptr[this->mSize] = data;
this->mSize++;
}
void Pushback(T&& data) //加一个取址符可以插入右值
{
if (this->mSize >= this->mCapacity)
{
cout << "数组已满" << endl;
return;
}
this->ptr[this->mSize] = data;
this->mSize++;
}
void Mysort() //正序排列
{
for (int i = 0; i < this->mSize; ++i)
{
for (int j = i + 1; j < this->mSize; ++j)
{
if (this->ptr[i] > this->ptr[j])
{
T temp = ptr[i];
ptr[i] = ptr[j];
ptr[j] = ptr[i];
}
}
}
}
void Myprtintf() //打印数组
{
for (int i = 0; i < this->mSize; ++i)
{
cout << this->ptr[i] << " ";
}
cout << endl;
}
bool Empty() //判断数组是否为空
{
if (this->mSize > 0)
{
cout<<"true";
return true;
}
else
{
cout << "false";
return false;
}
}
~MyArray()
{
if (this->ptr != NULL)
{
delete[] this->ptr;
}
}
public:
int mCapacity; //数组的容量
int mSize; //数组包含元素的个数
T* ptr; //指向数组元素的指针
};
void test01()
{
MyArray<int> arr1(10);
int a = 10;
arr1.Pushback(a);
arr1.Pushback(20);
arr1.Pushback(7);
arr1.Pushback(10);
arr1.Pushback(20);
/*for (int i = 0; i < arr1.mSize; ++i)
{
cout << arr1[i] << " ";
}
cout << endl;*/
arr1.Myprtintf();
arr1.Mysort();
/*for (int i = 0; i < arr1.mSize; ++i)
{
cout << arr1[i] << " ";
}*/
arr1.Myprtintf();
cout << "shan";
arr1.Myprtintf();
}
int main()
{
test01();
return 0;
}
实现数组的简单功能,后期变强后再完善
边栏推荐
- What are the functions and application industries of testing equipment development?
- Paoding solves the fiboracci sequence and knapsack problem - analyze the optimization process of the two problems in detail, and take you to understand dynamic programming from the most basic problem!
- Datart data visualization works are open source | chart plug-in works are all open source, which can be extracted through Baidu cloud download link
- Heap - principle to application - heap sort, priority queue
- 016:简单计算器
- Combinatorial summary
- 226. 翻转二叉树
- 01- fundamentals of automated testing -- (selenium eight part + environment configuration + eight positioning)
- What opportunities and challenges does the development of instrument control panel face?
- 148. 排序链表
猜你喜欢
随机推荐
好看又有趣的数据可视化图表制作,膜拜教程
Paoding solves the fiboracci sequence and knapsack problem - analyze the optimization process of the two problems in detail, and take you to understand dynamic programming from the most basic problem!
226. Flip binary tree
2021 popularization group summary
Common shortcut keys of robotframework
概率论-最大似然估计
D - AND and SUM (AtCoder Beginner Contest 238)
数据可视化应用安装部署 | 使用 datart 安装包和源码部署的常见问题教程
Subsequence
234. Palindrome linked list
Do you think sub database and sub table are really suitable for your system? Talk about how to select sub databases, sub tables and newsql
In depth analysis of multiple knapsack problem (Part 2)
016:简单计算器
How to complete the design of RGB lamp Bluetooth mesh module from 0 to 1
004:打印字符
Let you know the current situation and future development trend of wireless charging technology
005: storage space size of integer data type
概率论-假设检验
299. Guessing numbers game
148. Sorting linked list