C++简单实现动态数组

#pragma once
class ZVector
{
public:
	ZVector();
	~ZVector();
	ZVector(const ZVector &obj);
	ZVector& operator=(const ZVector &obj);

	// 操作
	// 获取数据
	int get(int index) const;
	int& operator[](int index);
	// 设置数据
	void set(int index,int value);
	// 放入数据
	bool push_back(int value);
	// 弹出数据
	int pop_back();
	// 是否为空
	bool empty() const;
	// 获取元素长度
	int size() const;
private:
	// 扩容
	bool resize_data();
	// 初始化
	bool Init();
	// 清除
	void Clear();
private:
	int* m_data; // 数据
	int m_size; // 实际元素个数
	int m_capacity; // 容量
};


#include "ZVector.h"
#include <assert.h>

#define DEFAULT_CAPACITY 16

/*
	默认构造函数
*/
ZVector::ZVector()
{
	assert(Init());
}

/*
	析构函数
*/
ZVector::~ZVector()
{
	Clear();
}

/*
	拷贝构造函数
*/
ZVector::ZVector(const ZVector& obj)
{
	assert(Init());
	for(int i = 0; i < obj.size(); i++){
		push_back(obj.m_data[i]);
	}
}

/*
	赋值构造函数
*/
ZVector& ZVector::operator=(const ZVector& obj)
{
	if (this == &obj) return *this;
	Clear();
	assert(Init());
	for (int i = 0; i < obj.size(); i++) {
		push_back(obj.m_data[i]);
	}
	return *this;
}

/*
	根据下标获取数据
*/
int ZVector::get(int index) const
{
	if(empty()){
		return -1;
	}
	if(index < 0 || this->m_size <= index){
		return -1;
	}
	return this->m_data[index];
}

/*
	重载中括号运算符
*/
int& ZVector::operator[](int index)
{
	return this->m_data[index];
}

/*
	根据下标修改数据
*/
void ZVector::set(int index, int value)
{
	if (empty()) {
		return;
	}
	if (index < 0 || this->m_size <= index) {
		return;
	}
	this->m_data[index] = value;
}

/*
	放入数据到末尾
*/
bool ZVector::push_back(int value)
{
	if (this->m_size == this->m_capacity) {
		// 扩容
		if (!resize_data()) {
			return false;
		}
	}
	this->m_data[this->m_size++] = value;
	return true;
}

/*
	弹出数据
*/
int ZVector::pop_back()
{
	int data = this->m_data[this->m_size--];
	if(this->m_size <= this->m_capacity / 4){
		// 进行缩容
		int newCapacity = this->m_capacity / 2;
		int* temp = new int[newCapacity];
		if (temp == nullptr) {
			return false;
		}
		for (int i = 0; i < this->m_size; i++) {
			temp[i] = this->m_data[i];
		}
		delete[] this->m_data;
		this->m_data = temp;
		this->m_capacity = newCapacity;
	}
	return data;
}

/*
	是否为空
*/
bool ZVector::empty() const
{
	return this->m_size == 0;
}

/*
	返回大小
*/
int ZVector::size() const
{
	return this->m_size;
}

/*
	扩容
*/
bool ZVector::resize_data()
{
	int newCapacity = this->m_capacity * 2;
	int* temp = new int[newCapacity];
	if(temp == nullptr){
		return false;
	}
	for(int i = 0; i < this->m_size; i++){
		temp[i] = this->m_data[i];
	}
	delete[] this->m_data;
	this->m_data = temp;
	this->m_capacity = newCapacity;
	return true;
}

/*
	初始化
*/
bool ZVector::Init()
{
	this->m_data = new int[DEFAULT_CAPACITY];
	if (this->m_data == nullptr) return false;
	this->m_size = 0; // 默认为0
	this->m_capacity = DEFAULT_CAPACITY; // 容量就是默认容量
	return true;
}

// 清除
void ZVector::Clear()
{
	if(this->m_data){
		delete[] this->m_data;
		this->m_data = nullptr;
		this->m_capacity = 0;
		this->m_size = 0;
	}
}

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <Windows.h>
#include "ZVector.h"

using namespace std;

int main()
{
	ZVector vector;
	vector.push_back(10);
	vector.push_back(11);
	vector.push_back(12);
	vector.push_back(13);
	vector.push_back(14);
	vector.push_back(15);
	vector.push_back(16);
	vector.push_back(17);
	vector.push_back(18);
	vector.push_back(19);
	vector.push_back(20);
	vector.push_back(21);
	vector.push_back(22);
	vector.push_back(23);
	vector.push_back(24);
	vector.push_back(25);
	vector.push_back(26);
	vector.push_back(27);
	vector.push_back(28);
	vector.push_back(29);
	vector.push_back(30);

	for(int i = 0; i < vector.size(); i++){
		cout << vector.get(i) << " ";
	}

	cout << endl;

	vector.set(0,50);
	vector.set(1,51);

	for (int i = 0; i < vector.size(); i++) {
		cout << vector.get(i) << " ";
	}

	cout << endl;

	vector.pop_back();
	vector.pop_back();
	vector.pop_back();
	vector.pop_back();
	vector.pop_back();
	vector.pop_back();
	vector.pop_back();
	vector.pop_back();
	vector.pop_back();
	vector.pop_back();
	vector.pop_back();
	vector.pop_back();
	vector.pop_back();
	vector.pop_back();
	vector.pop_back();
	vector.pop_back();

	for (int i = 0; i < vector.size(); i++) {
		cout << vector.get(i) << " ";
	}

	cout << endl;

	system("pause");
	return 0;
}

C++简单实现动态数组

上一篇:C++ Vector容器及三种遍历方式


下一篇:C++ STL标准模板库