二维动态数组类模板

本文介绍了如何使用C++模板实现一维和二维动态数组,并提供了详细的类定义和测试代码。通过运算符重载,使得使用自定义数组类如同内置类型一样便捷。代码已在vc++6.0上测试通过。
摘要由CSDN通过智能技术生成

C++提供了许多强大的机制来实现代码的高度复用、来使我们使用我们自已的类就像使用内置类型那样方便快捷。比如模板,运算符重载等等。模板好比如是一个大批量生产函数和类的工厂,使我们不用再去关心与数据类型相关的繁琐编程细节,把我们精力留给那些真正值得我们去认真思考的地方。而运算符重载则使我们的程序更直观更简洁,这不仅使我们更容易读懂我们的程序,而且使我们能以一种更为流畅的方式来表达我们的想法。就像上篇文章说到的,如果我们把动态分配的二维数组用类模板实现,并重载相应的操作符,我们就能十分方便的使用我们自己定义的数组类型了。今天我正好把以往的程序整理了一下,就一并贴了出来,看下面的程序。下面的两个类模板在vc++6.0上编译运行良好,测试程序也都得到正确的结果,如果在其他编译环境下有什么问题,欢迎给我留言。

第一个头文件Array.h是一维动态数组类模板:
//Array.h
#ifndef CARL_SEN_ARRAY_H
#define CARL_SEN_ARRAY_H

#include <iostream>
#include <cstdarg>
using std::out_of_range;
using std::ostream;

template<typename T>
class Array {
protected:
           unsigned int size;
           T* data;
public:
           //构造函数
           Array(unsigned int _size=0);
           Array(unsigned int count, T data1, ...);

           //复制控制
           Array(const Array<T>& array);
           Array& operator=(const Array<T>& array);
           ~Array() {
                       delete[] data;
           }
           //两种重载运算符
           T& operator[](unsigned int index);
           const T& operator[](unsigned int index) const;
           friend ostream& operator<<(ostream& os, const Array<T>& array);

           //get,set成员函数
           unsigned int getSize() const {
                      return size;
           }
           void setSize(unsigned int newSize);
};

template <typename T>
Array<T>::Array(unsigned int _size):data(new T[_size]), size(_size) {
           for(unsigned int i=0; i<size; ++i) {
                      data[i]=T();
           }
}

template <typename T>
Array<T>::Array(unsigned int count, T data1, ...):size(count), data(new T[count]) {
           va_list ap;
           va_start(ap, count);
           for(unsigned int i=0; i<size; ++i) {
                      data[i]=va_arg(ap, T);
           }
           va_end(ap);
}

template <typename T>
Array<T>::Array(const Array<T>& array):size(array.size), data(new T[array.size]) {
           for(unsigned int i=0; i<size; ++i) {
                      data[i]=array.data[i];
           }
}

template <typename T>
Array<T>& Array<T>::operator=(const Array<T>& array) {
           if(&array!=this) {
                      delete[] data;
                      data=new T[array.size];
                      size=array.size;
                      for(unsigned int i=0; i<size; ++i) {
                                 data[i]=array.data[i];
                      }
           }
           return *this;
}

template <typename T>
T& Array<T>::operator[](unsigned int index) {
           if(index>=siz

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值