描述
填写代码,创建Print模板类,要求对输入的字符串数组或整数数组,用模板类进行输出并自动换行
#include <iostream>
#include <string>
#include <numeric>
#include <algorithm>
using namespace std;
// 在此处补充你的代码
int main(){
string s[20];
int num[20];
int m,n;
while(cin >> m >> n){
for(int i=0; i<m; i++){
cin >> s[i];
}
accumulate(s, s+m, Print<string>(m));
for(int i=0; i<n; i++){
cin >> num[i];
}
accumulate(num, num+n, Print<int>(n));
}
}
输入
有多个输入样例
每个样例的第一行为两个整数m,n(m,n不超过20)
每个样例的第二行为m个字符串
每个样例的第三行为n个整数
输出
对每个样例输出两行
第一行为输入的字符串(去除空格)
第二行为输入的整数(去除空格)
样例输入
3 3
abc def hij
12 34 56
2 5
Peking University
20 18 05 1 3
样例输出
abcdefhij
123456
PekingUniversity
2018513
// Q759044.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>
#include <numeric>
#include <algorithm>
using namespace std;
// 在此处补充你的代码
template <class T>
class Print
{
public:
Print(int n)
{
}
void PrintIt(T val)
{
cout << val;
}
};
template<class T>
void accumulate(T * b, T * e, Print<T> p)
{
do
{
p.PrintIt(*b);
}
while (++b != e);
cout << endl;
}
int main(){
string s[20];
int num[20];
int m,n;
while(cin >> m >> n){
for(int i=0; i<m; i++){
cin >> s[i];
}
accumulate(s, s+m, Print<string>(m));
for(int i=0; i<n; i++){
cin >> num[i];
}
accumulate(num, num+n, Print<int>(n));
}
}