001:goodcopy

编写GoodCopy类模板,使得程序按指定方式输出
#include
using namespace std;

template
struct GoodCopy {
// 在此处补充你的代码
};

int a[200];
int b[200];
string c[200];
string d[200];

template
void Print(T s,T e) {
for(; s != e; ++s)
cout << * s << “,”;
cout << endl;
}

int main()
{
int t;
cin >> t;
while( t – ) {
int m ;
cin >> m;
for(int i = 0;i < m; ++i)
cin >> a[i];
GoodCopy()(a,a+m,b);
Print(b,b+m);
GoodCopy()(a,a+m,a+m/2);
Print(a+m/2,a+m/2 + m);

	for(int i = 0;i < m; ++i)
		cin >> c[i];
	GoodCopy<string>()(c,c+m,d);
	Print(c,c+m);
	GoodCopy<string>()(c,c+m,c+m/2);
	Print(c+m/2,c+m/2 + m);
}
return 0;

}
输入
第一行是整数 t,表示数据组数
每组数据:
第一行是整数 n , n < 50
第二行是 n 个整数
第三行是 n 个字符串
输出
将输入的整数原序输出两次,用","分隔
然后将输入的字符串原序输出两次,也用 ","分隔
样例输入
2
4
1 2 3 4
Tom Jack Marry Peking
1
0
Ted
样例输出
1,2,3,4,
1,2,3,4,
Tom,Jack,Marry,Peking,
Tom,Jack,Marry,Peking,
0,
0,
Ted,
Ted,

#include <iostream>
using namespace std;


template <class T>
struct GoodCopy {
public:
    void operator() (T* s, T* e, T* t) //重载()就是函数对象了呗, 还有在理解题目的意思上就花费了我半天的时间
    {
        if(t > e) //分两种情况,一个是内存相互影响, 一个不是
        {
            for(T* tt = s; tt != e; tt++) //这个就正常赋值
            {
                *t = *tt;
                t++;
            }
        }
        else  //这个要从后往前, 要不然从前往后,后面的还没赋的值已经被你前面覆盖了
        {
            T* t1 = e;
            t = t + (e-s);
            for(; t1 >= s; t1--, t--)
            {
                *t = *t1;
            }
        }
    }
};

int a[200];
int b[200];
string c[200];
string d[200];

template <class T>
void Print(T s,T e) {
	for(; s != e; ++s)
		cout << * s << ",";
	cout << endl;
}

int main()
{
	int t;
	cin >> t;
	while( t -- ) {
		int m ;
		cin >> m;
		for(int i = 0;i < m; ++i)
			cin >> a[i];
		GoodCopy<int>()(a,a+m,b);
		Print(b,b+m);
		GoodCopy<int>()(a,a+m,a+m/2);
		Print(a+m/2,a+m/2 + m);

		for(int i = 0;i < m; ++i)
			cin >> c[i];
		GoodCopy<string>()(c,c+m,d);
		Print(c,c+m);
		GoodCopy<string>()(c,c+m,c+m/2);
		Print(c+m/2,c+m/2 + m);
	}
	return 0;
}

做完了感觉挺简单的,做的时候啥都不是。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值