题目网址:http://cxsjsxmooc.openjudge.cn/2018t3springw7/
【1:简单的SumArray】
1 #include <iostream> 2 #include <string> 3 using namespace std; 4 template <class T> 5 T SumArray( 6 T* s,T* e){ 7 T tmp=*s; 8 for(T* i=s+1;i<e;i++){ 9 tmp+=*i; 10 } 11 return tmp; 12 } 13 int main() { 14 string array[4] = { "Tom","Jack","Mary","John"}; 15 cout << SumArray(array,array+4) << endl; 16 int a[4] = { 1, 2, 3, 4}; //提示:1+2+3+4 = 10 17 cout << SumArray(a,a+4) << endl; 18 return 0; 19 }
【2:简单的foreach】
1 #include <iostream> 2 #include <string> 3 using namespace std; 4 template<class T1,class T2> 5 void MyForeach(T1 begin,T1 end,T2 op){ 6 for(T1 i=begin;i<end;i++){ 7 op(*i); 8 } 9 } 10 void Print(string s) 11 { 12 cout << s; 13 } 14 void Inc(int & n) 15 { 16 ++ n; 17 } 18 string array[100]; 19 int a[100]; 20 int main() { 21 int m,n; 22 while(cin >> m >> n) { 23 for(int i = 0;i < m; ++i) 24 cin >> array[i]; 25 for(int j = 0; j < n; ++j) 26 cin >> a[j]; 27 MyForeach(array,array+m,Print); 28 cout << endl; 29 MyForeach(a,a+n,Inc); 30 for(int i = 0;i < n; ++i) 31 cout << a[i] << ","; 32 cout << endl; 33 } 34 return 0; 35 }
【3:简单的Filter】
1 #include <iostream> 2 #include <string> 3 using namespace std; 4 template<class T1,class T2> 5 T1 Filter(T1 begin,T1 end ,T1 arr,T2 op){ 6 int index=0; 7 for(T1 i=begin;i<end;i++){ 8 if(op(*i)){ 9 arr[index++]=*i; 10 } 11 } 12 return (arr+index); 13 } 14 bool LargerThan2(int n) 15 { 16 return n > 2; 17 } 18 bool LongerThan3(string s) 19 { 20 return s.length() > 3; 21 } 22 23 string as1[5] = {"Tom","Mike","Jack","Ted","Lucy"}; 24 string as2[5]; 25 int a1[5] = { 1,2,3,4,5}; 26 int a2[5]; 27 int main() { 28 string * p = Filter(as1,as1+5,as2,LongerThan3); 29 for(int i = 0;i < p - as2; ++i) 30 cout << as2[i]; 31 cout << endl; 32 int * p2 = Filter(a1,a1+5,a2,LargerThan2); 33 for(int i = 0;i < p2-a2; ++i) 34 cout << a2[i] << ","; 35 return 0; 36 }
【4:你真的搞清楚为啥 while(cin >> n) 能成立了吗?】
1 #include <iostream> 2 using namespace std; 3 class MyCin 4 { 5 public: 6 int flag; 7 MyCin():flag(0){ 8 } 9 istream& operator >>(int& x){ 10 cin>>x; 11 if(x==-1) 12 exit(0); 13 return cin; 14 } 15 16 }; 17 int main() 18 { 19 MyCin m; 20 int n1,n2; 21 while( m >> n1 >> n2) 22 cout << n1 << " " << n2 << endl; 23 return 0; 24 }
【5:山寨版istream_iterator】
1 #include <iostream> 2 #include <string> 3 4 using namespace std; 5 template <class T> 6 class CMyistream_iterator 7 { 8 public: 9 T value; 10 CMyistream_iterator(istream& i){ 11 i>>value; 12 } 13 void operator ++(int){ 14 cin>>value; 15 } 16 T operator *(){ 17 return value; 18 } 19 }; 20 21 22 23 int main() 24 { 25 int t; 26 cin >> t; 27 while( t -- ) { 28 CMyistream_iterator<int> inputInt(cin); 29 int n1,n2,n3; 30 n1 = * inputInt; //读入 n1 31 int tmp = * inputInt; 32 cout << tmp << endl; 33 inputInt ++; 34 n2 = * inputInt; //读入 n2 35 inputInt ++; 36 n3 = * inputInt; //读入 n3 37 cout << n1 << " " << n2<< " " << n3 << " "; 38 CMyistream_iterator<string> inputStr(cin); 39 string s1,s2; 40 s1 = * inputStr; 41 inputStr ++; 42 s2 = * inputStr; 43 cout << s1 << " " << s2 << endl; 44 } 45 return 0; 46 }
【6:这个模板并不难】
1 #include <iostream> 2 #include <string> 3 #include <cstring> 4 using namespace std; 5 template <class T> 6 class myclass { 7 public: 8 T* p; 9 int size; 10 myclass(T* arr,int n){ 11 size=n; 12 p=new T[n]; 13 for(int i=0;i<n;i++){ 14 p[i]=arr[i]; 15 } 16 } 17 ~myclass( ) { 18 delete [] p; 19 } 20 void Show() 21 { 22 for( int i = 0;i < size;i ++ ) { 23 cout << p[i] << ","; 24 } 25 cout << endl; 26 } 27 }; 28 int a[100]; 29 int main() { 30 char line[100]; 31 while( cin >> line ) { 32 myclass<char> obj(line,strlen(line));; 33 obj.Show(); 34 int n; 35 cin >> n; 36 for(int i = 0;i < n; ++i) 37 cin >> a[i]; 38 myclass<int> obj2(a,n); 39 obj2.Show(); 40 } 41 return 0; 42 }
【7:排序,又见排序!】
1 #include <iostream> 2 using namespace std; 3 4 bool Greater2(int n1,int n2) 5 { 6 return n1 > n2; 7 } 8 bool Greater1(int n1,int n2) 9 { 10 return n1 < n2; 11 } 12 bool Greater3(double d1,double d2) 13 { 14 return d1 < d2; 15 } 16 17 template <class T1,class T2> 18 void mysort( 19 T1 begin,T1 end,T2 op){ 20 T1 i,j; 21 for( i=begin;i<end;i++){ 22 for( j=i+1;j<end;j++){ 23 if(!op(*i,*j)){ 24 *i=*i+*j; 25 *j=*i-*j; 26 *i=*i-*j; 27 } 28 } 29 } 30 } 31 #define NUM 5 32 int main() 33 { 34 int an[NUM] = { 8,123,11,10,4 }; 35 mysort(an,an+NUM,Greater1); //从小到大排序 36 for( int i = 0;i < NUM; i ++ ) 37 cout << an[i] << ","; 38 mysort(an,an+NUM,Greater2); //从大到小排序 39 cout << endl; 40 for( int i = 0;i < NUM; i ++ ) 41 cout << an[i] << ","; 42 cout << endl; 43 double d[6] = { 1.4,1.8,3.2,1.2,3.1,2.1}; 44 mysort(d+1,d+5,Greater3); //将数组从下标1到下标4从小到大排序 45 for( int i = 0;i < 6; i ++ ) 46 cout << d[i] << ","; 47 return 0; 48 }