本帖最后由 yuxijian2020 于 2021-5-12 15:02 编辑
工具人来了
#include <iostream>
#include <initializer_list>
#include <cassert>
template<class T>
class T_Counter
{
public:
////////////////// 默认构造 //////////////////
T_Counter() { m_data = new T(); }
////////////////// 拷贝构造 //////////////////
explicit T_Counter(T_Counter<T>& other)
{
m_data = new T();
*m_data = *other.m_data;
}
explicit T_Counter(const T_Counter<T>& other)
{
m_data = new T();
*m_data = *other.m_data;
}
////////////////// 移动构造 //////////////////
explicit T_Counter(T_Counter<T>&& other)
{
m_data = other.m_data;
other.m_data = nullptr;
}
explicit T_Counter(T&& data)
{
m_data = new T();
*m_data = data;
}
////////////////// 单参数构造 //////////////////
explicit T_Counter(T& data)
{
m_data = new T();
*m_data = data;
}
////////////////// 析构 //////////////////
~T_Counter() { if (m_data) delete m_data; }
////////////////// 重载运算符 //////////////////
T_Counter<T>& operator=(const T_Counter<T>& other) { *m_data = *other.m_data; return *this; }
T_Counter<T> operator+(T_Counter<T>& other) { return T_Counter<T>(*m_data + *other.m_data); }
T_Counter<T> operator-(const T_Counter<T>& other) { return T_Counter<T>(*m_data - *other.m_data); }
T_Counter<T>& operator+=(const T_Counter<T>& other) { *m_data += *other.m_data; return *this; }
T_Counter<T>& operator-=(const T_Counter<T>& other) { *m_data -= *other.m_data; return *this; }
////////////////// 重载友元流操作符 //////////////////
friend std::ostream& operator<<(std::ostream& os, const T_Counter<T>& t) { os << *t.m_data; return os; }
friend std::istream& operator>>(std::istream& is, const T_Counter<T>& t) { is >> *t.m_data; return is; }
////////////////// 公开接口 //////////////////
//设置数据内容
void SetData(const T& data) { *m_data = data; }
//获取数据内容
T GetData() const { return *m_data; }
private:
T* m_data;
};
template<class T>
class T_Vector
{
public:
////////////////// 默认构造 //////////////////
explicit T_Vector() : m_vec(nullptr), m_size(0), m_capicity(0){}
explicit T_Vector(T_Vector<T>& other)
{
m_capicity = other.m_capicity;
m_size = other.m_size;
Allocator(other.m_size);
for (size_t i = 0; i < m_size; ++i)
m_vec[i] = other.m_vec[i];
}
explicit T_Vector(T data, size_t sum = 1)
{
//sum 大于0
assert(sum > 0);
Allocator(sum);
for (size_t i = 0; i < sum; ++i)
m_vec[i] = T_Counter<T>(data);
}
//初始化列表
explicit T_Vector(const std::initializer_list<T>& init)
{
if (init.size() == 0)
{
T_Vector();
return;
}
Allocator(init.size());
for (const auto& item : init)
Append(item);
}
////////////////// 移动构造 //////////////////
explicit T_Vector(T_Vector<T>&& other)
{
m_capicity = other.m_capicity;
m_size = other.m_size;
m_vec = other.m_vec;
other.m_capicity = 0;
other.m_size = 0;
other.m_vec = nullptr;
}
////////////////// 析构函数 //////////////////
~T_Vector()
{
if (m_vec)
delete[] m_vec;
m_size = 0;
m_capicity = 0;
}
////////////////// 重载运算符 //////////////////
T_Vector<T>& operator=(T_Vector& other)
{
m_capicity = other.m_capicity;
m_size = other.m_size;
if (m_vec) delete[] m_vec;
Allocator(m_capicity);
for (size_t i = 0; i < m_size; ++i)
m_vec[i] = other[i];
return *this;
}
const T_Counter<T>& operator[](size_t position) { assert(position < m_size); return m_vec[position]; }
////////////////// 公开接口 //////////////////
//是否为空
bool IsEmpty() const { if (m_size == 0) return true; else return false; }
//添加 sum 个数据,内容为 data
size_t Append(T data, size_t sum = 1)
{
assert(sum != 0);
if (m_size + 1 < m_capicity)
{
m_vec[m_size] = T_Counter<T>(data);
m_size += 1;
}
else
{
AddAllocator();
m_vec[m_size] = T_Counter<T>(data);
m_size += 1;
}
return m_size - 1;
}
//删除 pos 位置的数据
bool Erase(size_t pos)
{
if (pos >= m_size) return false;
for (size_t i = pos; i < m_size; ++i)
{
T_Counter<T> temp = m_vec[i + 1];
m_vec[i + 1] = m_vec[i];
m_vec[i] = temp;
}
m_size -= 1;
return true;
}
//获取存储的数据的总数
size_t Size() const { return m_size; }
//获得容量
size_t Capicity() const { return m_capicity; }
private:
//申请空间
void Allocator(size_t size) { m_vec = new T_Counter<T>[size]; m_capicity = size; m_size = 0; }
//重新申请空间
void ReAllocator(size_t size)
{
if (m_vec == nullptr) { Allocator(size); }
T_Counter<T>* temp = new T_Counter<T>[size];
for (size_t i = 0; i < m_size; ++i)
temp[i] = m_vec[i];
T_Counter<T>* t = m_vec;
delete[] t;
m_vec = temp;
m_capicity = size;
}
//增加空间
void AddAllocator()
{
if (m_vec == nullptr) Allocator(5);
ReAllocator(m_capicity * 2);
}
//数据指针
T_Counter<T>* m_vec;
//自身数据总数
size_t m_size;
//自身容量大小
size_t m_capicity;
};
|