C++基础学习笔记:自定义数组模板类

//!时间:2017年9月12日(周二)下午
//!内容:数组模板类
/*
修改:2017年9月13上午
成员方法中delete未正确匹配
改进:2017年9月13晚上
数组总量改为固定
*/
#define _CRTDBG_MAP_ALLOC
#include 
#include 
#include "ArrayTemplate.h"
#include "Arr.h"
#include 
int main()
{
	using namespace std;
{
		clock_t one = clock();
		Arr testOne(1);
		for (int i = 0; i < 10000; i++)
		{
			testOne.Add(5, 1);
		}
		clock_t two = clock();
		Array testTwo(1);
		for (int i = 0; i < 10000; i++)
		{
			testTwo.Add(5, 1);
		}
		clock_t three = clock();
		cout << "Add(5, 1)*10000固定大小arr: " << (double)(two - one) / CLOCKS_PER_SEC << endl;
		cout << "Add(5, 1)*10000不固定Array: " << (double)(three - two) / CLOCKS_PER_SEC << endl< test;
	for (int i = 0; i < test.Size(); i++)
	{
		test[i] = i;
		cout << "  " << test[i];
	}
	cout << endl << "间隔插入数字6:";
	for (int i = 5; i >= 0; i--)
	{
		test.Add(6,i);
	}
	for (int i = 0; i < test.Size(); i++)
	{
		cout << "  " << test[i];
	}
	cout << endl << "第一个数字4位于:位" << test.Search(4);
	test.Delete(9);//删除位9的4
	test.Reset(4, 2);//将位2设置为4
	cout << endl << "Reset(4, 2)后4位于:位" << test.Search(4) << endl << endl;

	cout << "测试char *:" << endl;
	cout << "声明一个长度为3的char型数组并赋值:\n";
	Arr str("aa");
	for (int i = 0; i < str.Size(); i++)
	{
		cout << str[i]<<"  ";
	}
	str.Add("add");
	str.Reset("reset", 1);
	cout << "\n现在数组内容为:\n";
	for (int i = 0; i  ss("string");
	for (int i = 3; i >= 0; i--)
	{
		ss.Add("aa", i);
	}
	ss.Reset("111", 1);
	for (int i = 0; i 
#pragma once
template 
class Arr
{
private:
	static const int LIMIT = 11000;
	int count = n;
	T *arr;
public:
	Arr()//构造
	{
		if (n > LIMIT) throw "丢一个异常";
		arr = new T[LIMIT];
	}
	explicit Arr(const T&t)//构造
	{
		if (n > LIMIT) throw "丢一个异常";
		arr = new T[LIMIT];
		for (int i = 0; i < n; i++)
			arr[i] = t;
	}
	~Arr()//析构
	{
		delete[]arr;
	}
	//下标访问
	T&operator[](int i);
	//插入or增加一个元素
	bool Add(const T&t, int i = -1);
	//删除某个元素
	bool Delete(int i = -1);
	//重置某个元素
	bool Reset(const T&t, int i);
	//查询某个元素第一个下标
	int Search(const T&t);
	//查询数组长度
	int Size();
};
template 
T& Arr::operator[](int i)
{
	if (i<0 || i >= count)
	{
		throw "out of limits";
	}
	return arr[i];
}
template 
bool Arr::Add(const T&t, int i)
{
	if (count + 1 > LIMIT)
	{
		return false;
	}
	else if (i < 0 || i >= count)
	{
		count++;
		arr[count - 1] = t;
		return true;
	}
	else if (i >= 0 && i i; x--)
			arr[x] = arr[x - 1];
		arr[i] = t;
		return true;
	}
	else return false;
}
template 
bool Arr::Delete(int i)
{
	if (i < 0 && count>0)
	{
		count--;
		return true;
	}
	else if (i >= 0 && count>0 && i
bool Arr::Reset(const T&t, int i)
{
	if (i >= 0 && i < count)
	{
		arr[i] = t;
		return true;
	}
	else return false;
}
template 
int Arr::Search(const T&t)
{
	for (int i = 0; i
int Arr::Size()
{
	return count;
}


//!使用模板实现一个自定义的数组类,
//!能够新增、修改、删除、使用下标访问数据、
//!根据数据返回数据组中第一个符合的小标、
//!获取数组中当前存储的数据个数
#pragma once
#include 

template 
class Array
{
private:
	int count=n;
	T *ar;
	T *temp;
public:
	int theN()
	{
		return n;
	}
	Array() 
	{
		ar = new T[count];
		temp = nullptr;
	}
	explicit Array(const T&t);//初始化为同一个数值
	~Array()
	{
		delete[]ar;
		ar = nullptr;
		if (nullptr!=temp)
		{
			delete[]temp;
		}
		temp = nullptr;
	}
	//下标访问
	T&operator[](int i);
	//插入or增加一个元素
	bool Add(const T&t, int i = -1);
	//重置某个元素
	bool Reset(const T&t,int i);
	//删除某个元素
	bool Delete(int i=-1);
	//查询某个元素第一个下标
	int Search(const T&t);
	//查询数组长度
	int Size();
};
template 
Array::Array(const T&t)
{
	ar = new T[count];
	for (int i = 0; i < n; i++)
		ar[i] = t;
}
template 
T& Array::operator[](int i)
{
	if (i<0||i >= count)
	{
		throw "out of limits";
	}
	return ar[i];
}
template 
bool Array::Add(const T&t,int i)
{
	if (i < 0||i>=count )
	{
		count++;
		temp = new T[count];
		for (int x = 0; x < count-1; x++)
			temp[x] = ar[x];
		delete []ar;
		ar = new T[count];
		for (int x = 0; x < count-1; x++)
			ar[x] = temp[x];
		ar[count - 1] = t;
		delete []temp;
		temp = nullptr;
		return true;
	}
	else if (i >= 0 && i
bool Array::Reset(const T&t,int i)
{
	if (i >= 0 && i < count)
	{
		ar[i] = t;
		return true;
	}
	else return false;
}
template 
bool Array::Delete( int i)
{
	if (i < 0&&count>0)
	{
		count--;
		temp = new T[count];
		for (int x = 0; x < count; x++)
			temp[x] = ar[x];
		delete []ar;
		ar = new T[count];
		for (int x = 0; x < count; x++)
			ar[x] = temp[x];
		delete []temp;
		temp = nullptr;
		return true;
	}
	else if(i>=0&&count>0&&i
int Array::Search(const T&t)
{
	for(int i=0;i
int Array::Size()
{
	return count;
}




你可能感兴趣的:(C++,没有结局的开始)