使用C++类模板实现动态数组
2018-03-25 16:40
344 查看
#include <iostream> #include <string> using namespace std; //使用类模板实现动态数组! template<typename T, int N> class Array{ private: T* m_array; int m_len; public: //构造函数,给数组申请空间 Array() : m_array(new T ), m_len(N){} //拷贝构造函数 template<int X> Array(const Array<T, X>& that) { m_len = that.size(); m_array = new T[m_len]; if (m_array == NULL) { cout << "have not enough free memory" << endl; return; } memset(m_array, 0, m_len); memcpy(m_array, that.get_data(), m_len*sizeof(T)); } //重载 = 运算符,使得数组之间能够实现 = 操作! template<int Y> Array& operator = (const Array<T, Y>& other) { if (this != &other) { //为保证程序的可靠性,应该先用一个临时变量将数据复制成功之后再改变被赋值数组的值, //这样可以保证在程序运行出错时,原数组不会受影响 array = new T[m_len]; if (array == NULL) { cout << "have not enough free memory" << endl; return; } memset(array, 0, other.size()); memcpy(array, other.get_data(), other.size()*sizeof(T)); delete[] m_array; m_array = array; m_len = other.size(); } return *this; } //重载下标运算符,使得下标运算符具有越界检测功能! T& operator[](int index) { if (index > m_len - 1) { cout << "Array out of range, 0 to " << (m_len - 1) << " is legitimate scope." << endl; return m_array[0]; //因为return的是m_array[0],所以如果是在执行 = 操作,则数组第一位元素值就会发生变化! } return m_array[index]; } //重载下标运算符,保证常数组也能实现相关操作 T& operator[](unsigned int index) const { if (index > m_len - 1) { cout << "Array out of range, 0 to " << (m_len - 1) << " is legitimate scope." << endl; return m_array[0]; } return m_array[index]; } //用来扩大数组的容量 Array* add_memory(unsigned int len) { //增加一个中间变量 T* temp = new T[m_len + len]; if (temp == NULL) { cout << "have not enough free!" << endl; return this; } //将原来的数据存储进这个中间变量中 memset(temp, 0, (m_len + len) * sizeof(T)); memcpy(temp, m_array, m_len*sizeof(T)); //删除原来的数组指针,并将中间变量temp赋值给成员变量m_array; delete[] m_array; m_array = temp; m_len = m_len + len; return this; } int size() const { return m_len; } T* get_data() const { return m_array; } friend ostream& operator<<(ostream& os, const Array<T, N>& array) { for (int i = 0; i < array.size(); i++) { os << array[i] << " "; //保证输出格式 if (!(i + 1 % 5)) { os << endl; } } return os; } }; int main() { Array<int, 5> b; b[0] = 1; b[1] = 3; b[2] = 4; b[3] = 5; b[4] = 6; b[7] = 8; cout << b[0] << endl; cout << b << endl; Array<int, 2> a(b); cout << a << endl; cout << a.size() << endl; cout << b.size() << endl; b.add_memory(10); cout << b.size() << endl; cout << b << endl; Array<int, 5> c; c = b; cout << c << endl; cout << c.size() << endl; b[20] = 20; system("pause"); return 0; }
相关文章推荐
- 数据结构之动态数组 (C++类模板实现)
- html+js+PHP(使用了smarty模板技术)+mysql实现二级动态下拉列表(select)
- 实现动态自增数组并使用选择排序法排序
- C++类模板 实现两栈共享数组空间的算法 《数据结构》(北京科海) 自己摘抄完成
- 使用freemarker实现静态模板维护及动态内容展示案例
- 使用数组和模板实现的队列
- 使用C语言实现vector动态数组的实例分享
- html+js+PHP(使用了smarty模板技术)+mysql实现二级动态下拉列表(select)
- assign() 功能的实现 数组分配到模板。使用 extract() 函数。 变量分配到模板。使用 compact()
- Java版C语言版简单使用静态语言实现动态数组的方法
- 用hibernate实现动态查询+分页的模板,另附struts2的标签在分页中的使用
- 用模板实现动态数组
- 模板容器类的实现一(基于动态数组)
- 使用动态数组的方式实现栈
- 使用内存管理函数实现动态数组
- 使用C++模板技术实现类的动态注册和获取
- html+js+PHP(使用了smarty模板技术)+mysql实现二级动态下拉列表(select)
- 使用 Velocity 实现客户端和服务器端模板
- 使用模板实现ASP代码与页面分离
- 使用Java动态代理实现AOP