POJ C++程序设计 编程题#1 编程作业—文件操作与模板

编程题#1

来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩。)

注意: 总时间限制: 1000ms 内存限制: 65536kB

描述

实现一个三维数组模版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 <iostream>
using namespace std;
// 在此处补充你的代码
int main()
{
    CArray3D<int> 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;
    };
};

 1 #include <iostream>
 2 using namespace std;
 3 // 在此处补充你的代码
 4 template <class T>
 5 class CArray3D {
 6 public:
 7     template <class T1>
 8     class CArray2D {
 9     private:
10         T1 *a;
11         int i,j;
12     public:
13         CArray2D() {a = NULL;}
14         CArray2D(int a1, int a2):i(a1),j(a2) {
15             a = new T1[i*j];
16         }
17         ~CArray2D() {
18             if (a != NULL) delete []a;
19         }
20         T1 *operator[](int a1) {
21             return a + a1*j;
22         }
23     };
24     CArray3D() {array2D = NULL;}
25     CArray3D(int a1, int a2, int a3) {
26         array2D = new CArray2D<T>*[a1];
27         for (int m = 0; m < a1; ++m) {
28             array2D[m] = new CArray2D<T>(a2, a3);
29         }
30     }
31     CArray2D<T> &operator[](int i) {
32         return *array2D[i];
33     }
34     ~CArray3D() {
35         if (array2D != NULL) delete []array2D;
36     }
37 private:
38     CArray2D<T> **array2D;
39 };
40 int main()
41 {
42     CArray3D<int> a(3,4,5);
43     int No = 0;
44     for( int i = 0; i < 3; ++ i )
45         for( int j = 0; j < 4; ++j )
46             for( int k = 0; k < 5; ++k )
47                 a[i][j][k] = No ++;
48     for( int i = 0; i < 3; ++ i )
49         for( int j = 0; j < 4; ++j )
50             for( int k = 0; k < 5; ++k )
51                 cout << a[i][j][k] << ",";
52     return 0;
53 }

 

转载于:https://www.cnblogs.com/dagon/p/4776905.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值