当前位置: 首页 > news >正文

c++ code:(13)io and template

/*
填写模板 PrintArray,使得程序输出结果是:
TomJackMaryJohn
10

*/ 

#include <iostream>
#include <string>
using namespace std;
template <class T>
T SumArray(
//your code starts here
T * begin, T * end) {
	T tmp = * begin;
	++ begin;
	for(;  begin != end ; ++ begin)
		tmp += * begin;
	return tmp;
//your code ends here	
}
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函数
 
输入:
多组数据
每组数据第一行是两个整数 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,
 
*/

#include <iostream>
#include <string>
using namespace std;
//your code starts here
template <class T1, class T2 >
void MyForeach( T1 s,T1 e, T2 op) 
{
	for(;s != e; ++s) {
		op(*s);
	}
}
//your code ends here
	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;
	} 

/*编写Filter模板,使得程序产生指定输出
不得编写 Filter函数
输入
无
输出

MikeJackLucy
3,4,5,

输入样例
无
输出样例
MikeJackLucy
3,4,5,
*/ 
#include <iostream>
#include <string>
using namespace std;

//your code starts here
template <class T1, class T2>
T1 Filter( T1 s,T1 e, T1 s2, T2 op) 
{
	for(;s != e; ++s) {
		if( op(*s)) {
			* s2 = * s;
			++ s2;
		}
	}
	return s2;
}
//your code ends here

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;
} 

/*
读入两个整数,输出两个整数 ,直到碰到-1
输入
多组数据,每组一行,是两个整数

输出
对每组数据,原样输出 
当碰到输入中出现-1 时,程序结束 
输入中保证会有 -1 

输入样例
12 44
344 555
-1
2 3

输出样例 
12 44
344 555
*/
#include <iostream>
using namespace std;
class MyCin
{
	//your code starts here
    bool valid;    
    public:
        MyCin():valid(true) { }
        operator bool( ) { //重载类型强制转换运算符 bool
            return valid; 
        }
        MyCin & operator >> (int & n)
        {
            cin >> n;
            if( n == -1 )
            	valid = false;
            
            return * this;
        }
//your code ends here        
};
int main()
{
    MyCin m;
    int n1,n2;
    while( m >> n1 >> n2) 
        cout  << n1 << " " << n2 << endl;
    return 0;
}

/*
程序填空,按要求输出
输入
第一行是整数t,表示有t组数据
每组数据一行,三个整数加两个字符串。字符串是不含空格的

输出:
对每组数据,输出二行 
在第一行输出第一个数
第二行原样输出输入的内容

输入样例 
2
79 90 20 hello me
12 34 19 take up

输出样例:
79
79 90 20 hello me
12
12 34 19 take up

提示:
C++标准模板库 istream_iterator模版使用说明:

其构造函数执行过程中就会要求输入,然后每次执行++,则读取输入流中的下一个项目,执行 * 则返回上次从输入流中读取的项目。例如,下面程序运行时,就会等待用户输入数据,输入数据后程序才会结束:
#include <iostream>
#include <iterator>
using namespace std;
int main()  { 
	 istream_iterator<int> inputInt(cin);
	 return 0;  
}

下面程序运行时,如果输入 12   34  程序输出结果是: 12,12
#include <iostream>
#include <iterator>
using namespace std;
int main()  
{ 
	 istream_iterator<int> inputInt(cin);
	 cout << * inputInt << "," << * inputInt << endl;
	 return 0;  
}

下面程序运行时,如果输入 12   34   56程序输出结果是: 12,56
#include <iostream>
#include <iterator>
using namespace std;
int main()  
{ 
	 istream_iterator<int> inputInt(cin);
	 cout << * inputInt << "," ;
	 inputInt ++;
	 inputInt ++;
	 cout << * inputInt;
	 return 0;  
}
 

*/


#include <iostream>
#include <string>

using namespace std;
template <class T>
class CMyistream_iterator
{
//your code starts here	
	istream &  r;
	T v;
	public:
		T operator *() {
			return v;	
		}
		CMyistream_iterator( istream & rr):r(rr) {
			r >> v ;
		}
		void operator ++ (int) {
			 r >> v ;
		}
//your code ends here		
};



int main()  
{ 
	int t;
	cin >> t;
	while( t -- ) {
		 CMyistream_iterator<int> inputInt(cin);
		 int n1,n2,n3;
		 n1 = * inputInt; //读入 n1
		 int tmp = * inputInt;
		 cout << tmp << endl;
		 inputInt ++;   
		 n2 = * inputInt; //读入 n2
		 inputInt ++;
		 n3 = * inputInt; //读入 n3
		 cout << n1 << " " << n2<< " " << n3 << " ";
		 CMyistream_iterator<string> inputStr(cin);
		 string s1,s2;
		 s1 = * inputStr;
		 inputStr ++;
		 s2 = * inputStr;
		 cout << s1 << " " << s2 << endl;
	}
	 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,
*/

#include <iostream>
#include <string>
#include <cstring>
using namespace std;
template <class T>  
class myclass { 
//your code starts here
	T * p;;
	int size;
public:
	myclass ( T a [], int n)  
	
	{
		p = new T[n];
		for( int i = 0;i < n;i ++ )
			p[i] = a[i];
		size = n;
	} 
//your code ends here	
	~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;
}


/*
3) 自己编写一个能对任何类型的数组进行排序的mysort函数模版,可以如下使用:
4 8 10 11 123
123 11 10 8 4
1.400000 1.200000 1.800000 3.100000 3.200000 2.100000

提示:
1> an 和 an + NUM 的类型显然是一样的,都是 int *
2> mysort里用第二个参数减去第一个参数,就能知道要排序的元素有几个
3> mysort(an,an+NUM,Greater1) 会导致 Greater1在mysort内部被调用,用来比较元素大小,因此回忆一下函数指针的用法
	
*/
#include <iostream>
using namespace std;

bool Greater2(int n1,int n2) 
{
	return n1 > n2;
}
bool Greater1(int n1,int n2) 
{
	return n1 < n2;
}
bool Greater3(double d1,double d2)
{
	return d1 < d2;
}

template <class T1,class T2>
void mysort(
//your code starts here
 T1 * start , T1 * end, T2 myless )
{
	int size = end - start;
	for( int i = size -1;i >= 0 ; --i ) {
		for( int j = 0; j < i ; ++j ) {
			if(  myless( start[j+1],start[j] )) {
				T1 tmp = start[j];
				start[j] = start[j+1];
				start[j+1] = tmp; 
			}
		}
	}
}
/*答案2 :
template<class T>
void Swap( T & a, T & b)
{
	T tmp;
	tmp = a;
	a = b;
	b = tmp;
}

template <class T1,class T2>
void mysort( T1  start , T1  end, T2 myless )
{
	int size = end - start;
	for( int i = size -1;i >= 0 ; --i ) {
		for( int j = 0; j < i ; ++j ) {
			if(  myless( * ( start + j+1), * (start+j) )) {
				Swap(* ( start + j+1), * (start+j) );
			}
		}
	}
}
答案 3 
template <class T1,class T2>
void mysort( T1  start , T1  end, T2 myless )
{
	int size = end - start;
	for( int i = size -1;i >= 0 ; --i ) {
		for( int j = 0; j < i ; ++j ) {
			if(  myless( * ( start + j+1), * (start+j) )) {
				auto tmp = * ( start+j);
				* ( start +j ) = * ( start + j+1);
				* ( start + j+1)  = tmp; 
			}
		}
	}
}

*/

//your code ends here

#define NUM 5
int main()
{
    int an[NUM] = { 8,123,11,10,4 };
    mysort(an,an+NUM,Greater1); //从小到大排序 
    for( int i = 0;i < NUM; i ++ )
       cout << an[i] << ",";
    mysort(an,an+NUM,Greater2); //从大到小排序 
    cout << endl;
    for( int i = 0;i < NUM; i ++ )
        cout << an[i] << ","; 
    cout << endl;
    double d[6] = { 1.4,1.8,3.2,1.2,3.1,2.1};
    mysort(d+1,d+5,Greater3); //将数组从下标1到下标4从小到大排序 
    for( int i = 0;i < 6; i ++ )
         cout << d[i] << ","; 
	return 0;
}

 

相关文章:

  • 英文论文写作素材
  • 王梓坤院士的学术思想
  • 使用EI、SCI搜索 (使用 websci中的endnote online引用 )
  • 玩转word 和 mathtype 的结合
  • paper 格式
  • 计算思维
  • 程序中的一些思想
  • 深入 理解char * ,char ** ,char a[ ] ,char *a[] 的区别
  • STL MAP四种插入方法
  • c++ STL函数大全(一):vector
  • c++ STL函数大全(二):map
  • c++ STL函数大全(三):queue
  • c++ STL函数大全(四):string
  • c++ STL函数大全(五):set
  • data structure summary: (一):linklist
  • [LeetCode] Wiggle Sort
  • 【划重点】MySQL技术内幕:InnoDB存储引擎
  • AWS实战 - 利用IAM对S3做访问控制
  • EventListener原理
  • JS进阶 - JS 、JS-Web-API与DOM、BOM
  • PHP 程序员也能做的 Java 开发 30分钟使用 netty 轻松打造一个高性能 websocket 服务...
  • 聚类分析——Kmeans
  • 批量截取pdf文件
  • 巧用 TypeScript (一)
  • 吐槽Javascript系列二:数组中的splice和slice方法
  • 小程序滚动组件,左边导航栏与右边内容联动效果实现
  • 移动端高清、多屏适配方案
  • ​Spring Boot 分片上传文件
  • ​ssh-keyscan命令--Linux命令应用大词典729个命令解读
  • $refs 、$nextTic、动态组件、name的使用
  • (2)(2.4) TerraRanger Tower/Tower EVO(360度)
  • (python)数据结构---字典
  • (力扣题库)跳跃游戏II(c++)
  • (六)c52学习之旅-独立按键
  • (免费领源码)python#django#mysql公交线路查询系统85021- 计算机毕业设计项目选题推荐
  • (四)TensorRT | 基于 GPU 端的 Python 推理
  • (原創) 如何動態建立二維陣列(多維陣列)? (.NET) (C#)
  • (终章)[图像识别]13.OpenCV案例 自定义训练集分类器物体检测
  • (转贴)用VML开发工作流设计器 UCML.NET工作流管理系统
  • (转载)OpenStack Hacker养成指南
  • (最全解法)输入一个整数,输出该数二进制表示中1的个数。
  • *ST京蓝入股力合节能 着力绿色智慧城市服务
  • .NET 8 编写 LiteDB vs SQLite 数据库 CRUD 接口性能测试(准备篇)
  • .NET CORE Aws S3 使用
  • .net core 调用c dll_用C++生成一个简单的DLL文件VS2008
  • .NET框架设计—常被忽视的C#设计技巧
  • .net使用excel的cells对象没有value方法——学习.net的Excel工作表问题
  • .net项目IIS、VS 附加进程调试
  • .skip() 和 .only() 的使用
  • @ 代码随想录算法训练营第8周(C语言)|Day57(动态规划)
  • [20150321]索引空块的问题.txt
  • [⑧ADRV902x]: Digital Pre-Distortion (DPD)学习笔记
  • [ABP实战开源项目]---ABP实时服务-通知系统.发布模式
  • [AIGC] Kong:一个强大的 API 网关和服务平台
  • [Android]How to use FFmpeg to decode Android f...