C++ 模板编程练习

填写模板 PrintArray,使得程序输出结果是: TomJackMaryJohn 10 不得编写SumArray函数

#include <iostream>
#include <string>
using namespace std;
template <class T>
T SumArray(
// 在此处补充你的代码
}
int main() {
	string array[4] = { "Tom","Jack","Mary","John"};
	cout << SumArray(array,array+4) << endl;
	int a[4] = { 1, 2, 3, 4};  //提示:1+2+3+4 = 10
	cout << SumArray(a,a+4) << endl;
	return 0;
}

输入

输出

TomJackMaryJohn
10

样例输入

样例输出

TomJackMaryJohn
10

来源

Guo Wei

前闭后开,使用指针编程

#include <iostream>
#include <string>
using namespace std;
// 前开后闭区模板编程
template <class T>
T SumArray(T* start,T* end){           // 传入两个指针
	// 在此处补充你的代码
	T* p = start;
	T res = *start;
	p++;
	while (p != end) {
		res += *p;
		p++;
	}
	return res;
}
int main() {
	string array[4] = { "Tom","Jack","Mary","John" };
	cout << SumArray(array, array + 4) << endl;
	int a[4] = { 1, 2, 3, 4 };  //提示:1+2+3+4 = 10
	cout << SumArray(a, a + 4) << endl;
	return 0;
}

编写MyForeach模板,使程序按要求输出 不得编写 MyForeach函数

#include <iostream>
#include <string>
using namespace std;
// 在此处补充你的代码
void Print(string s)
{
	cout << s;
}
void Inc(int & n)
{
	++ n;
}
string array[100];
int a[100];
int main() {
	int m,n;
	while(cin >> m >> n) {
		for(int i = 0;i < m; ++i)
			cin >> array[i];
		for(int j = 0; j < n; ++j)
			cin >> a[j];
		MyForeach(array,array+m,Print);		 
		cout << endl;
		MyForeach(a,a+n,Inc);		 
		for(int i = 0;i < n; ++i)
			cout << a[i] << ",";
		cout << endl;
	}
	return 0;
}

输入

多组数据

每组数据第一行是两个整数 m 和 n ,都不超过 50

第二行是m个不带空格的字符串
第三行是 n个整数

输出

对每组数据
第一行输出所有输入字符串连在一起的结果
第二行输出输入中的每个整数加1的结果

样例输入

3 4
Tom Mike Jack
1 2 3 4
1 2
Peking
100 200

样例输出

TomMikeJack
2,3,4,5,
Peking
101,201,

来源

Guo Wei

仍然是前闭后开定义,第三个参数是函数的指针,注意这里只有一个模板参数不行 ,所以用两个模板参数

#include <iostream>
#include <string>
using namespace std;
// 在此处补充你的代码
template<class T1,class T2>
void MyForeach(T1* start, T1* end, void (*f)(T2)) {
	T1* p = start;
	while (p != end) {
		f(*p);
		p++;
	}
}

void Print(string s)
{
	cout << s;
}
void Inc(int & n)
{
	++n;
}

int a[100];
int main() {

	string array[100];
	int m, n;
	while (cin >> m >> n) {
		for (int i = 0; i < m; ++i)
			cin >> array[i];
		for (int j = 0; j < n; ++j)
			cin >> a[j];
		MyForeach(array, array + m, Print);
		cout << endl;
		MyForeach(a, a + n, Inc);
		for (int i = 0; i < n; ++i)
			cout << a[i] << ",";
		cout << endl;
	}
	return 0;
}

总时间限制: 

1000ms

内存限制: 

65536kB

// 在此处补充你的代码

描述

程序填空,输出指定结果

#include <iostream>
#include <string>
#include <cstring>
using namespace std;
template <class T>  
class myclass {
~myclass( ) {
		delete [] p;
	}
	void Show()
	{
		for( int i = 0;i < size;i ++ ) {
			cout << p[i] << ",";
		}
		cout << endl;
	}
};
int a[100];
int main() {
	char line[100];
	while( cin >> line ) {
		myclass<char> obj(line,strlen(line));;
		obj.Show();
		int n;
		cin >> n;
		for(int i = 0;i < n; ++i)
			cin >> a[i];
		myclass<int> obj2(a,n);
		obj2.Show();
	}
	return 0;
}

输入

多组数据。每组第一行是一个不含空格的字符串
第二行是整数n
第三行是n个整数

输出

对每组数据,先依次输出输入字符串的每个字母,并且在每个字母后面加逗号
然后依次再输出输入的n个整数 ,在每个整数后面加逗号

样例输入

Tom 
3
3 4 5
Jack
4
1 2 3 4

样例输出

T,o,m,
3,4,5,
J,a,c,k,
1,2,3,4,

来源

Guo Wei

#include <iostream>
#include <string>
#include <cstring>
using namespace std;
template <class T>
class myclass {
	// 在此处补充你的代码
private:
	T* p;
	int size;
public:
	myclass(T *p_, int n) {
		size = n;
		p = new T[sizeof(T)*n+1];
		memcpy(p, p_, n*sizeof(T));
	}

	~myclass() {
		delete[] p;
	}
	void Show()
	{
		for (int i = 0; i < size; i++) {
			cout << p[i] << ",";
		}
		cout << endl;
	}
};
int a[100];
int main() {
	char line[100];
	while (cin >> line) {
		myclass<char> obj(line, strlen(line));;
		obj.Show();
		int n;
		cin >> n;
		for (int i = 0; i < n; ++i)
			cin >> a[i];
		myclass<int> obj2(a, n);
		obj2.Show();
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/wwxy1995/article/details/88830434
今日推荐