实现一个三维数组模版CArray3D,可以用来生成元素为任意类型变量的三维数组

描述

实现一个三维数组模版CArray3D,可以用来生成元素为任意类型变量的三维数组,使得下面程序输出结果是:

0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,

注意,只能写一个类模版,不能写多个。

#include 
using namespace std;
// 在此处补充你的代码
int main()
{
    CArray3D a(3,4,5);
    int No = 0;
    for( int i = 0; i < 3; ++ i )
        for( int j = 0; j < 4; ++j )
            for( int k = 0; k < 5; ++k )
                a[i][j][k] = No ++;
    for( int i = 0; i < 3; ++ i )
        for( int j = 0; j < 4; ++j )
            for( int k = 0; k < 5; ++k )
                cout << a[i][j][k] << ",";
return 0;
}

输入

输出

0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,

样例输入

样例输出

0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,

提示

提示:类里面可以定义类,类模版里面也可以定义类模版。例如:

class A
{
    class B {

    };
};

template
class S
{
    T x;
    class K {
        T a;
    };
};

用指针写了个。没写析构函数。不大明白的。没有用到提示

template
class CArray3D
{
private:
	T *** _array;
	int _r,_c,_l;
public:
	CArray3D(int r, int c, int l)
	{
		_array = new T **[r];
		for(int i = 0;i

————————————————————————————————————————————————————————————————————————————

模板类里定义模板类

template
class CArray3D
{
public:
	template 
	class CArray2D
	{
	public:
		template 
		class CArray1D	
		{
		public:
			CArray1D()
			{
				a = NULL;
			}
			void set(int l)
			{
				a = new T[l];
			}
			~CArray1D()
			{
				if(a) delete []a;
			}
			T2 & operator [] (int index)
			{
				return a[index];
			}
		
		private:
			T2 *a;		
		};
		CArray2D()
		{
			a1 = NULL;
		}
		void set(int c, int l)
		{
			a1 = new CArray1D[c];
			for(int i = 0;i & operator [] (int index)
		{
			return a1[index];
		}
	private:
		CArray1D * a1;
	};
	CArray3D()
	{
		a2 = NULL;
	}
	CArray3D(int r, int c, int l)
	{
		a2 = new CArray2D[r];
		for(int i=0;i & operator [] (int index)
	{
		return a2[index];
	}
private:
	CArray2D * a2;
};


你可能感兴趣的:(实现一个三维数组模版CArray3D,可以用来生成元素为任意类型变量的三维数组)