C++ 模板函数 001:简单的SumArray 002 简单的foreach 003 简单的Filter 006 这个模板并不难

001:简单的SumArray

描述
填写模板 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

函数的功能
将给定数组的每个元素相加,返回相加的值,类型为模板类型

T SumArray(            //模板函数 
// 在此处补充你的代码
T* s, T* e
){
T a;
int i=0;
while(s!=e){       //全部加起来 
	a+=*s++;
}
return a;
//

002:简单的foreach

描述
编写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,

函数作用
类似map函数,对给定起始的列表中每个元素都进行给定的函数操作。

// 在此处补充你的代码
template <class T, class pred>
//void MyForeach(T *s,T *e, pred op){
void MyForeach(T s,T e, pred op){
	while (s!=e){
		op(*s++);                //无返回值返回,直接操作就行 
	}
} 
// 

003:简单的Filter

描述
编写Filter模板,使得程序产生指定输出 不得编写 Filter函数

#include <iostream>
#include <string>
using namespace std;
// 在此处补充你的代码
bool LargerThan2(int n)
{
	return n > 2;
}
bool LongerThan3(string s) 
{
	return s.length() > 3;
}

string as1[5] = {"Tom","Mike","Jack","Ted","Lucy"};
string as2[5];
int  a1[5] = { 1,2,3,4,5};
int a2[5];
int main() {
	string * p = Filter(as1,as1+5,as2,LongerThan3);
	for(int i = 0;i < p - as2; ++i)
		cout << as2[i];
	cout << endl; 
	int * p2 = Filter(a1,a1+5,a2,LargerThan2);
	for(int i = 0;i < p2-a2; ++i)
		cout << a2[i] << ",";
	return 0;
}

输入

输出

MikeJackLucy
3,4,5,

函数功能
筛选列表中满足给定条件的成员。

#include <iostream>
#include <string>
using namespace std;
// 在此处补充你的代码
template <class T, class pred>
T Filter(T s, T  e, T x, pred op){
	while (s!=e){
		if (op(*s)){
			*x++=*s++;
		}
		else ++s;
	}
	return x;
} 

// 
bool LargerThan2(int n)
{
	return n > 2;                // 
}
bool LongerThan3(string s) 
{
	return s.length() > 3;        //长度>3 
}

string as1[5] = {"Tom","Mike","Jack","Ted","Lucy"};
string as2[5];
int  a1[5] = { 1,2,3,4,5};
int a2[5];
int main() {
	string * p = Filter(as1,as1+5,as2,LongerThan3);        
	//Filter 需返回string *类型,故T不能为T *; 
	for(int i = 0;i < p - as2; ++i)        //p为s2的end 
		cout << as2[i];                  //MikeJackLucy 
	cout << endl; 
	int * p2 = Filter(a1,a1+5,a2,LargerThan2);
	for(int i = 0;i < p2-a2; ++i)
		cout << a2[i] << ",";            //3,4,5, 
	return 0;
}

易错点
因为右下面的赋值函数
string * p = Filter(as1,as1+5,as2,LongerThan3);
所以Filter的返回类型为 stirng类型,
所以T后面全部加
也是ok的,
T的统一性很重要

template <class T, class pred>
T *Filter(T *s, T  *e, T* x, pred op){
	while (s!=e){
		if (op(*s)){
			*x++=*s++;
		}
		else ++s;
	}
	return x;
} 

006:这个模板并不难

描述
程序填空,输出指定结果

#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,

模板类
查看main函数中调用模板类的操作
将输入的一行字符放入类对象,通过类对象的show函数输出。
添加一个构造函数即可。

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

//
~myclass( ) {         //析构函数 
		delete [] p;          //动态数组p 
	}
	void Show()         //show函数 
	{
		for( int i = 0;i < size;i ++ ) {
			cout << p[i] << ",";
		}
		cout << endl;
	}
};
int a[100];
int main() {
	char line[100];
	while( cin >> line ) {        //Tom 
		myclass<char> obj(line,strlen(line));;   //构造函数(T 数组,int 长度) 
		obj.Show();                 // T,o,m 
		int n;
		cin >> n;              //3
		for(int i = 0;i < n; ++i)
			cin >> a[i];       //3 4 5
		myclass<int> obj2(a,n);      
		obj2.Show();
	}
	return 0;
}
发布了77 篇原创文章 · 获赞 3 · 访问量 3050

猜你喜欢

转载自blog.csdn.net/BLUEsang/article/details/105177833
今日推荐