SDUSTOJ1572 Problem F: 数组类(I)

Description

封装一个整型数组类,用于存储整数和处理的相关功能,支持以下操作:

  1. Array::Array()无参构造方法:创建一个空数组对象。
  2. Array::size()方法:返回Array对象中元素个数。
  3. Array::get(int n)方法:按格式从输入读取n元素。
  4. 下标运算符:返回下标所指的元素。

你设计一个数组类Array,使得main()函数能够正确运行。 函数调用格式见append.cc。
append.cc中已给出main()函数

Input

输入的第一个整数n,表示有n组测试数据。
后面的每行以一个整数k开头,表示后面有k个整数。

Output

把输入的数组,输出出来。每行数据对应一个输出。格式见sample。

Sample Input

4
2 10 20
1 0
0
3 1 2 3

Sample Output

10 20
0
  
1 2 3

HINT

Append Code

append.cc,

标程

#include <bits/stdc++.h>
using namespace std;
 
class Array{
    private:
        int l, *a;
    public:
        Array():l(0) {}
        int size() {return l;}
        void get(int s) {
            a = new int[s+1]; 
            l = s;
            for(int i = 0; i < s; i++) cin >> a[i];
        }
        int operator[](int n) {return a[n];}
        ~Array() {delete[] a;}
 
};
 
int main()
{
    int cases;
    Array arr;
    cin >> cases;
    for(int ca = 1; ca <= cases; ca++)
    {
        int len;
        cin >> len;
        arr.get(len);
        for(int i = 0; i < arr.size(); i++)
            if(i + 1 == arr.size())
                cout << arr[i];
            else
                cout << arr[i] << " ";
        cout << endl;
    }
}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值