C++简单数组模板

#include<iostream>
#include <string>
#define MAXSIZE 100
using namespace std;
class Student

{
public:
	string name;
	string ID;
	int score;
public:
	/** 带参数的构造函数*/
	Student(string name, string ID, int score)
	{
		this->name = name;
		this->ID = ID;
		this->score = score;
	}

	/**深拷贝*/
	Student(const Student& obj)
	{
		this->name = obj.name;
		this->ID = obj.ID;
		this->score = obj.score;
	}

	/** 默认构造函数*/
	Student()
	{
		this->name = "";
		this->ID = "";
		this->score = 0;
	}
	
	/** 显示学生对象的信息*/
	void Show()
	{
		cout << "name :" << name << ",ID:" << ID << ",score :" << score;
	}

	/**重载[] 运算符*/
	Student& operator [](int index)
	{
		return *this;
	}

	/** 重载= 运算符*/
	void  operator = (const Student& obj)
	{
		this->name = obj.name;
		this->ID = obj.ID;
		this->score = obj.score;
	}
	void operator =(string key)
	{
		this->name = key;
	}
	
	/** 重载== 运算符*/
	bool operator == (string key)
	{
		if (this->name == key)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	/**重载<  运算符*/
	bool operator <(Student& obj)
	{
		return this->score < obj.score;
	}
	
	/**重载<<  运算符 类内声明 类外定义*/
	friend ostream& operator <<(ostream& out, Student& stu);
};

ostream& operator <<(ostream& out, Student& stu)
{
	out << "name :" << stu.name << ",ID:" << stu.ID << ",score :" << stu.score;
	return out;
}


/**模板*/
template <typename T,typename Tc=T>
class Array
{
public:
	T* m_space;     /*数组*/
	int length;       /*数组长度*/
	int last;				/*最后一个元素的下标*/
public:
	/** 带默认参数的构造函数 */
	//根据参数size确定数组的长度length 在此未对size进行判断
	Array(int size = 0)
	{
		m_space = new T[MAXSIZE];
		length = size;
		last = -1;
	}

	/** 深拷贝*/
	Array(const Array& obj)
	{
		m_space = new T[MAXSIZE];
		length = obj.length;
		last = obj.last;
		for (int i = 0; i <= last; i++)
		{
			m_space[i] = obj[i];
		}
	}

	/** 析构函数*/
	~Array()
	{
		if (m_space != NULL)
		{
			delete[]m_space;
			m_space = NULL;
			length = 0;
			last = -1;
		}
	}

	/** 元素的添加*/
	void append(const T& obj)
	{
		if (last == (length - 1))
		{
			cout << "添加错误->数组已满" << endl;
		}
		else
		{
			m_space[++last] = obj;
		}
	}
	
	/**元素的显示*/
	
	void Show()
	{
		if (sizeof(m_space[0]) !=60)         //判断数组里的元素是不是Student类型
		{
			for (int i = 0; i <= last; i++)
			{
				cout << m_space[i] << "\t";
			}
			cout << endl;
		}
		else
		{
			for (int i = 0; i <= last; i++)
			{
				cout << m_space[i] << endl;
			}
		}
		return;
	}
	

	/**元素的修改*/
	void Modify_info(const Tc pre,const Tc post)
	{
		int i = search(pre);
		if (i == -1)
		{
			cout << "修改错误->错误数据" << endl;
		}
		else
		{
			m_space[i] = post;       //默认修改名字
		}
		return;
	}

	/**元素的排序*/
	void Sort()
	{
		for (int i = 0; i <= last; i++)
		{
			for (int j = i+1; j <= last; j++)
			{
				if (m_space[i] < m_space[j])
				{
					T tmp = m_space[i];
					m_space[i] = m_space[j];
					m_space[j] = tmp;
				}
			}
		}
		return;
	}

	
	/**元素的查找*/
		int search(Tc key)
		{
			for (int i = 0; i <= last; i++)
			{
				if (m_space[i] == key)
				{
					return i;
				}
			}
			return -1;
		}

		/**元素的删除*/
		void Delete(Tc key)
		{
		int index = search(key);              /**查找待删除元素的位置*/
		if (index == -1)
		{
			cout << "删除错误->没有这个元素" << endl;
			return;
		}
		else
		{
			for (int i = index; i < last; i++)
			{
				m_space[i] = m_space[i + 1];
			}
			this->last--;
		}
		return;
	}

	/**重载[]运算符*/
	T& operator [](int index)
	{
		return m_space[index];
	}
};
int main()
{
  Array<int> arr(10);              //创建int型数组 长度为10
  Array<Student, string> stu(10);  //创建Student类型数组,长度为10 第二个类型参数一定要修改
  return 0;   //主函数里自己调用函数
}
发布了11 篇原创文章 · 获赞 2 · 访问量 619

猜你喜欢

转载自blog.csdn.net/Cheng_XZ/article/details/103222608