【C++】Array 动态数组类的设计

前言

今天学习了类模板的知识,趁热打铁写了一个动态数组类,有插入、删除、构造等成员函数(和STL实现稍有不同),恳请大家批评指正!

class Array - 类定义

template<class T>
class Array
{
private:
	T *_list;
	size_t _maxsize;
	size_t _size;
	
public:
	Array(LL __sz = 50);
	Array(const Array<T> &__a);
	Array(const vector<T> &__v);
	~Array();
	
	Array<T>& operator = (const Array<T> &__rhs);
	T& operator [] (LL __n);
	const T& operator [] (LL __n) const;
	operator T*();
	operator const T*() const;
	
	T* begin() const;
	T* end() const;
	
	size_t size() const;
	size_t max_size() const;
	T* data() const;
	
	void resize(LL __sz);
	void clear();
	void assign(T *__begin, T *__end);
	
	void push_back(const T &__a);
	T pop_back();
	void insert(LL __pos, const T &__a);
	T erase(LL __pos);
	void erase(LL __first, LL __last);
};

Array::Array(LL __sz) - 长度构造函数(默认)

template<class T> Array<T>::Array(LL __sz)
{
	assert(__sz >= 0);
	_maxsize = (size_t)(__sz);
	_size = 0;
	_list = new T[_maxsize]{};
}

Array::Array(const vector<T> &__v) - vector构造数组

template<class T> Array<T>::Array(const vector<T> &__v)
{
	_maxsize = (size_t)(__v.size());
	_size = 0;
	_list = new T[_maxsize]{};
	auto it = __v.begin();
	for (; it!=__v.end(); it++)
		push_back(*it);
}

Array::Array(const Array<T> &__a) - 拷贝构造

因为涉及到动态指针的问题,我们要自己写深拷贝来解决。

template<class T> Array<T>::Array(const Array<T> &__a)
{
	*this = __a;    //利用了重载的 = 运算符
}

Array::~Array() - 析构函数

template<class T> Array<T>::~Array()
{
	if (_list != nullptr)
		delete[] _list;
}

 Array<T>& Array::operator = (const Array<T> &__rhs) - 重载赋值运算符

template<class T> Array<T>& Array<T>::operator = (const Array<T> &__rhs)
{
	if (&__rhs != this)
	{
		if (_maxsize != __rhs._maxsize)
		{
			delete[](_list);
			_size = __rhs._size;
			_maxsize = __rhs._maxsize;
			_list = new T[_maxsize]{};
		}
		for (size_t i=0; i<_maxsize; i++)
			_list[i] = __rhs._list[i];
	}
	return *this;
}

T& Array<T>::operator [] (LL __n) - 重载下标运算符

template<class T> T& Array<T>::operator [] (LL __n)
{
	assert(__n>=0 && __n<_maxsize);
	return _list[(size_t)(__n)];
}

const T& Array<T>::operator [] (LL __n) - 重载下标运算符(常量版)

template<class T> const T& Array<T>::operator [] (LL __n) const
{
	assert(__n>=0 && __n<_maxsize);
	return _list[(size_t)(__n)];
}

Array<T>::operator T* () - 重载类型转换运算符

这样数组名就可以当作数组的首地址指针来使用。

template<class T> Array<T>::operator T* ()
{
	return _list;
}

Array<T>::operator const T* () - 重载类型转换运算符(常量版)

template<class T> Array<T>::operator const T* () const
{
	return _list;
}

T* Array<T>::begin() - 获取首指针

模仿了STL的迭代器操作,但这里返回的单纯只是指针。 

template<class T> T* Array<T>::begin() const
{
	return _list;
}

T* Array<T>::end() - 获取尾指针

模仿了STL的迭代器操作,但这里返回的单纯只是指针。

template<class T> T* Array<T>::end() const
{
	return _list + _size;
}

size_t Array<T>::size() - 返回数组元素个数

template<class T> size_t Array<T>::size() const
{
	return _size;
}

size_t Array<T>::max_size() - 返回数组最大长度

template<class T> size_t Array<T>::max_size() const
{
	return _maxsize;
}

T* Array<T>::data() - 返回底层数组的首指针

template<class T> T* Array<T>::data() const
{
	return _list;
}

void Array<T>::resize(LL __sz) - 调整数组长度

template<class T> void Array<T>::resize(LL __sz)
{
	assert(__sz >= 0);
	if ((size_t)(__sz) == _maxsize)
		return ;
	T *newL = new T[__sz]{};
	LL len = min(size_t(__sz), _maxsize);
	for (LL i=0; i<len; i++)
	{
		newL[i] = _list[i];
	}
	delete[] _list;
	_list = newL;
	_maxsize = (size_t)(__sz);
	_size = min(size_t(__sz), _size);
}

结语

Array动态数组类的分享就到这里啦!更多精彩内容请移步专栏~

掰掰ヾ(•ω•`)o

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的 C++ DynamicArray 类的实现,包括增加、删除、插入、排序等功能: ```c++ #include <iostream> #include <algorithm> class DynamicArray { private: int* data; int size; int capacity; public: DynamicArray(int capacity = 10) { this->capacity = capacity; size = 0; data = new int[capacity]; } ~DynamicArray() { delete[] data; } void add(int val) { if (size == capacity) { capacity *= 2; int* new_data = new int[capacity]; std::copy(data, data + size, new_data); delete[] data; data = new_data; } data[size++] = val; } void remove(int index) { if (index < 0 || index >= size) throw std::out_of_range("Index out of range!"); for (int i = index; i < size - 1; ++i) data[i] = data[i + 1]; --size; } void insert(int index, int val) { if (index < 0 || index > size) throw std::out_of_range("Index out of range!"); if (size == capacity) { capacity *= 2; int* new_data = new int[capacity]; std::copy(data, data + size, new_data); delete[] data; data = new_data; } for (int i = size; i > index; --i) data[i] = data[i - 1]; data[index] = val; ++size; } void sort() { std::sort(data, data + size); } int getSize() const { return size; } int getCapacity() const { return capacity; } int& operator[](int index) { if (index < 0 || index >= size) throw std::out_of_range("Index out of range!"); return data[index]; } const int& operator[](int index) const { if (index < 0 || index >= size) throw std::out_of_range("Index out of range!"); return data[index]; } }; ``` 这个 DynamicArray 类支持动态增长,可以使用 add() 方法在末尾添加元素,remove() 方法删除指定索引的元素,insert() 方法在指定索引处插入元素,sort() 方法对所有元素进行排序。getSize() 和 getCapacity() 方法分别返回元素数量和数组容量。还重载了 [] 运算符,使得可以通过索引访问元素。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值