构建小型的泛型容器主要目的是实践一下,模板类编程,函数指针的使用等。

一、该容器的作用,仿照vector动态数组,构建泛型的动态数组。

二、为容器实现成员函数,包括排序函数。

三、函数介绍:

1、尾部插入函数(函数原型:void push_back(T element)):如果容器容量不大于插入后的元素个数,正常插入;若插入后的元素个数大于容器容量,调用扩容函数进行扩容,再添加元素。实现详情如下:

template<typename T>
void miniVector<T>::push_back(T element) {
	if (this->size == this->capacity) {
		this->capacity <<= 1;
		this->reserve(capacity);
	}
	this->data[size] = element;
	size++;
}

2、插入函数(函数原型void insert(int pointer, T element)):插入函数支持非尾部插入,插入的时间效率与插入位置线性相关;若插入后的元素个数大于容器容量,处理方法与尾部插入函数相同。

template<typename T>
void miniVector<T>::insert(int pointer, T element) {
	if (this->size == this->capacity) {
		this->capacity <<= 1;
		this->reserve(capacity);
	}
	for (int i = size; i >= pointer; i--) {
		this->data[i] = this->data[i - 1];
	}
	this->data[pointer] = element;
	this->size++;
}

3、扩容函数(函数原型:void  reserve(int reserve_capacity)):扩容函数输入的参数为扩容后的大小,若扩容后容量大小小于元素个数大小,则改变元素个数以适应容器容量;

template<typename T>
void miniVector<T>::reserve(int reserve_capacity) {
	T *reserve_data = this->data;
	data = new T[reserve_capacity];
	if (this->size > reserve_capacity) {
		this->size = reserve_capacity;
	}
	for (int i = 0; i < this->size; i++) {
		this->data[i] = reserve_data[i];
	}
	delete[]reserve_data;
}

由于删除、返回元素个数等函数比较简单,不一一介绍了

4、堆排序函数(函数原型:void HeapSort(int start_pointer, int end_pointer,bool(*compare)(const T a, const T b) = mcmp);):实现可自定义排序规则的参数为函数指针,指针设置默认值,代表默认为升序排序。由于该函数为类成员函数,所以函数指针的默认值不能为某个对象的默认函数,必须为该类的静态成员函数(static bool my_compare(const T a, const T b);),该静态成员需要定义为private以达到对外透明的目的。

//静态函数,默认的比较函数
static bool my_compare(const T a, const T b);
//静态的函数指针
static bool(*mcmp)(const T a, const T b);

//静态成员函数实现默认的排序规则
template<typename T>
bool miniVector<T>::my_compare(const T a, const T b) {
	if (a < b)return true;
	return false;
}
//静态成员函数指针的赋值(类的静态成员必须在类外初始化)
template<typename T>
bool (*miniVector<T>::mcmp)(const T a,const T b) = my_compare;

//堆排序的实现
template<typename T>
void miniVector<T>::HeapSort(int start_pointer, int end_pointer,
	bool(*compare)(const T a, const T b)) {
	for (int i = ((end_pointer + 1) >> 1) - 1; i >= start_pointer; i--) {
		adjust(i, end_pointer, compare);
	}
	for (int i = start_pointer; i < end_pointer; i++) {
		swap(start_pointer, end_pointer - i);
		adjust(start_pointer, end_pointer - i - 1, compare);
	}
}

//建立堆的函数
template<typename T>
void miniVector<T>::adjust(int index, int length,
	bool(*compare)(const T a, const T b)) {
	int left = (index << 1) + 1;
	int right = (index << 1) + 2;
	int max_index = index;
	if (left <= length &&
		compare(this->data[max_index], this->data[left])
		)max_index = left;
	if (right <= length &&
		compare(this->data[max_index], this->data[right])
		)max_index = right;
	if (index != max_index) {
		swap(index, max_index);
		adjust(max_index, length, compare);
	}
}

//定义交换函数,函数体较小所以在此定义相当于内联函数
	void swap(int i_index, int j_index) {
		T temp = this->data[i_index];
		this->data[i_index] = this->data[j_index];
		this->data[j_index] = temp;
	}

整个容器的实现:

头文件:

//miniVector.h
#pragma once
#ifndef MINIVECTOR_H	//避免源文件的多次定义
#define MINIVECTOR_H
//头文件声明miniVector
template<typename T>
class miniVector{		
private:
	int capacity = 10;	//定义容量
	int size = 0;		//定义元素数量
	T *data = new T[capacity];		//泛型类型的数据
	//声明用于建立堆的函数
	void adjust(int index, int length, bool(*compare)(const T a, const T b));
	//定义交换函数,函数体较小所以在此定义相当于内联函数
	void swap(int i_index, int j_index) {
		T temp = this->data[i_index];
		this->data[i_index] = this->data[j_index];
		this->data[j_index] = temp;
	}
	//静态函数,默认的比较函数
	static bool my_compare(const T a, const T b);
	//静态的函数指针
	static bool(*mcmp)(const T a, const T b);
public:
	//构造函数
	miniVector() {};
	//重载可变容量的构造函数
	miniVector(int user_defined_capacity) {
		this->capacity = user_defined_capacity;
		T *delete_data = data;
		delete[]delete_data;
		data = new T[this->capacity];
	}
	//查询函数,返回值为泛型模板
	T find(int pointer);
	//扩容函数
	void reserve(int reserve_capacity);
	//尾部插入函数
	void push_back(T element);
	//插入函数
	void insert(int pointer, T element);
	//更新元素的函数
	void update(int pointer, T update_element);
	//删除函数
	void erase(int pointer);
	//返回元素个数的函数,设为内联函数
	int getSize() {
		return this->size;
	}
	//堆排序函数,第三个参数为函数指针,默认升序函数,可改变排序规则
	void HeapSort(int start_pointer, int end_pointer,
		bool(*compare)(const T a, const T b) = mcmp);
};
#endif

实现该头文件的源文件:

//RealizeMiniVector.cpp
#include<iostream>
#include"miniVecotr.h"

//实现函数的源文件

//查询函数实现
template<typename T>
T miniVector<T>::find(int pointer) {
	if (pointer >= size || pointer < 0) {
		return NULL;
	}
	return this->data[pointer];
}

//扩容函数的实现
template<typename T>
void miniVector<T>::reserve(int reserve_capacity) {
	T *reserve_data = this->data;
	data = new T[reserve_capacity];
	if (this->size > reserve_capacity) {
		this->size = reserve_capacity;
	}
	for (int i = 0; i < this->size; i++) {
		this->data[i] = reserve_data[i];
	}
	delete[]reserve_data;
}

//尾部插入函数的实现
template<typename T>
void miniVector<T>::push_back(T element) {
	if (this->size == this->capacity) {
		this->capacity <<= 1;
		this->reserve(capacity);
	}
	this->data[size] = element;
	size++;
}

//插入函数的实现
template<typename T>
void miniVector<T>::insert(int pointer, T element) {
	if (this->size == this->capacity) {
		this->capacity <<= 1;
		this->reserve(capacity);
	}
	for (int i = size; i >= pointer; i--) {
		this->data[i] = this->data[i - 1];
	}
	this->data[pointer] = element;
	this->size++;
}

//更新元素函数的实现
template<typename T>
void miniVector<T>::update(int pointer, T update_element) {
	if (pointer >= this->size || pointer < 0) {
		//未处理
	}
	else {
		this->data[pointer] = update_element;
	}
}

//删除元素的函数
template<typename T>
void miniVector<T>::erase(int pointer) {
	if (pointer >= this->size || pointer < 0) {
		//未处理
	}
	else {
		for (int i = pointer; i < this->size - 1; i++) {
			this->data[i] = this->data[i + 1];
		}
		this->size--;
	}
}

//建立堆的函数
template<typename T>
void miniVector<T>::adjust(int index, int length,
	bool(*compare)(const T a, const T b)) {
	int left = (index << 1) + 1;
	int right = (index << 1) + 2;
	int max_index = index;
	if (left <= length &&
		compare(this->data[max_index], this->data[left])
		)max_index = left;
	if (right <= length &&
		compare(this->data[max_index], this->data[right])
		)max_index = right;
	if (index != max_index) {
		swap(index, max_index);
		adjust(max_index, length, compare);
	}
}

//静态成员函数实现默认的排序规则
template<typename T>
bool miniVector<T>::my_compare(const T a, const T b) {
	if (a < b)return true;
	return false;
}
//静态成员函数指针的赋值
template<typename T>
bool (*miniVector<T>::mcmp)(const T a,const T b) = my_compare;
//堆排序的实现
template<typename T>
void miniVector<T>::HeapSort(int start_pointer, int end_pointer,
	bool(*compare)(const T a, const T b)) {
	for (int i = ((end_pointer + 1) >> 1) - 1; i >= start_pointer; i--) {
		adjust(i, end_pointer, compare);
	}
	for (int i = start_pointer; i < end_pointer; i++) {
		swap(start_pointer, end_pointer - i);
		adjust(start_pointer, end_pointer - i - 1, compare);
	}
}

四、使用介绍:

由于使用模板类,在声明miniVector时需要声明类型。

1、声明方法:

miniVector<int> my_Vector;	//直接声明进行使用


miniVector<int> *my_int_Vector = new miniVector<int>(20);	
	//可以通过构造函数指定容器的容量

miniVector<char> *my_char_Vector = new miniVector<char>();
	//可以调用无参的构造函数,不设定容量

 2、排序方法的使用:

函数原型:void HeapSort(int start_pointer, int end_pointer,bool(*compare)(const T a, const T b) = mcmp);参数一,为排序开始的下标;参数二,为排序结束的下标;参数三,排序规则的方法,可省略,省略为默认升序。

//HeapSort(排序首下标,排序未下标,排序规则的方法(可省略));
	my_int_Vector->HeapSort(0, my_int_Vector->getSize()-1);

//自定义的排序规则函数
bool my_compare(const int a, const int b) {
	if (a > b)return true;
	return false;
}
bool(*mcmp)(const int a, const int b) = my_compare;

my_int_Vector->HeapSort(0, my_int_Vector->getSize() - 1,mcmp);

实现效果:

给容器添加env_STL