• 作业7-输入输出与模板


    1.简单的SumArray

     1 //填写模板 PrintArray,使得程序输出结果是: TomJackMaryJohn 10 不得编写SumArray函数
     2 
     3 #include <iostream>
     4 #include <string>
     5 using namespace std;
     6 template <class T>
     7 T SumArray(T * begin, T * end){
     8 // 在此处补充你的代码
     9     T sum = begin[0];
    10     for(int i = 1; i < end-begin; i++){
    11         sum += begin[i];
    12     }
    13     return sum;
    14 }
    15 int main() {
    16     string array[4] = { "Tom","Jack","Mary","John"};
    17     cout << SumArray(array,array+4) << endl;
    18     int a[4] = { 1, 2, 3, 4};  //提示:1+2+3+4 = 10
    19     cout << SumArray(a,a+4) << endl;
    20     return 0;
    21 }

    其实这个可以不用i的……下面的题目就没有用i

    2.简单的foreach

     1 //编写MyForeach模板,使程序按要求输出 不得编写 MyForeach函数
     2 #include <iostream>
     3 #include <string>
     4 using namespace std;
     5 // 在此处补充你的代码
     6 template<class T, class Func>
     7 void MyForeach(T s, T e, Func out){
     8     for(; s!=e; ++s)
     9         out(*s);
    10 }
    11 void Print(string s)
    12 {
    13     cout << s;
    14 }
    15 void Inc(int & n)
    16 {
    17     ++ n;
    18 }
    19 string array[100];
    20 int a[100];
    21 int main() {
    22     int m,n;
    23     while(cin >> m >> n) {
    24         for(int i = 0;i < m; ++i)
    25             cin >> array[i];
    26         for(int j = 0; j < n; ++j)
    27             cin >> a[j];
    28         MyForeach(array,array+m,Print);         
    29         cout << endl;
    30         MyForeach(a,a+n,Inc);         
    31         for(int i = 0;i < n; ++i)
    32             cout << a[i] << ",";
    33         cout << endl;
    34     }
    35     return 0;
    36 }

    这里就没有用i = =

    3.简单的Filter

     1 //编写Filter模板,使得程序产生指定输出 不得编写 Filter函数
     2 #include <iostream>
     3 #include <string>
     4 using namespace std;
     5 // 在此处补充你的代码
     6 template<class T, class Func>
     7 T Filter(T begin, T end, T temp, Func op){
     8     for(; begin!=end; begin++){
     9         if(op(*begin)) 
    10             *(temp++) = *begin;
    11     }
    12     return temp; //应当返回的是末尾 
    13 }
    14 
    15 bool LargerThan2(int n)
    16 {
    17     return n > 2;
    18 }
    19 bool LongerThan3(string s) 
    20 {
    21     return s.length() > 3;
    22 }
    23 
    24 string as1[5] = {"Tom","Mike","Jack","Ted","Lucy"};
    25 string as2[5];
    26 int  a1[5] = { 1,2,3,4,5};
    27 int a2[5];
    28 int main() {
    29     string * p = Filter(as1,as1+5,as2,LongerThan3);
    30     for(int i = 0;i < p - as2; ++i)
    31         cout << as2[i];
    32     cout << endl; 
    33     int * p2 = Filter(a1,a1+5,a2,LargerThan2);
    34     for(int i = 0;i < p2-a2; ++i)
    35         cout << a2[i] << ",";
    36     return 0;
    37 }

    注意审题,i<p-as2,说明返回的是数组末尾指针

    4.你真的搞清楚为啥 while(cin >> n) 能成立了吗?

    我没搞清楚……

     1 //读入两个整数,输出两个整数 ,直到碰到-1
     2 #include <iostream>
     3 using namespace std;
     4 class MyCin
     5 {
     6 // 在此处补充你的代码
     7 private:
     8     bool status;
     9 public:
    10     MyCin(){status = true;}
    11     MyCin & operator>>(int & n){ //这里必须是引用,要不然没有真正的输入到这个变量里面 
    12         if(!status) return *this;
    13         cin>>n;
    14         if(n==-1) status = false;
    15         return *this;
    16     }
    17     operator bool(){ //重载类型转换运算符 
    18         return status;
    19     }
    20 };
    21 int main()
    22 {
    23     MyCin m;
    24     int n1,n2;
    25     while( m >> n1 >> n2) 
    26         cout  << n1 << " " << n2 << endl;
    27     return 0;
    28 }

    首先就是重载>>的参数一定要是引用,然后就是status这个状态变量,然后就是重载类型转换运算符。反正就是挺巧妙的我觉得……好好看看吧。

    5.山寨版istream_iterator

     1 //模仿C++标准模板库istream_iterator用法,实现CMyistream_iterator使得程序按要求输出
     2 #include <iostream>
     3 #include <string>
     4 
     5 using namespace std;
     6 template <class T>
     7 class CMyistream_iterator
     8 {
     9 // 在此处补充你的代码
    10     T *  num;
    11     istream & is;
    12 public: 
    13     CMyistream_iterator(istream & is_):is(is_){
    14         num = NULL;
    15     } 
    16     T & operator *(){ //注意返回值类型为T的引用 
    17         if(num) return *num;
    18         num = new T; //不要忘 
    19         is >> *num;
    20         return *num; 
    21     }
    22     void operator++(int){ //后置的++ 
    23         if(num){
    24             delete num;
    25             num = NULL;
    26         }
    27     }
    28     ~CMyistream_iterator(){
    29         if(num) delete num;
    30     }
    31 };
    32 
    33 
    34 
    35 int main()  
    36 { 
    37     int t;
    38     cin >> t;
    39     while( t -- ) {
    40          CMyistream_iterator<int> inputInt(cin);
    41          int n1,n2,n3;
    42          n1 = * inputInt; //读入 n1
    43          int tmp = * inputInt;  //不执行输入操作,因为没有++ 
    44          cout << tmp << endl;
    45          inputInt ++;   
    46          n2 = * inputInt; //读入 n2
    47          inputInt ++;
    48          n3 = * inputInt; //读入 n3
    49          cout << n1 << " " << n2<< " " << n3 << " ";
    50          CMyistream_iterator<string> inputStr(cin);
    51          string s1,s2;
    52          s1 = * inputStr;
    53          inputStr ++;
    54          s2 = * inputStr;
    55          cout << s1 << " " << s2 << endl;
    56     }
    57      return 0;  
    58 }

    提示

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

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

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

    5.这个模板并不难

     1 //程序填空,输出指定结果
     2 #include <iostream>
     3 #include <string>
     4 #include <cstring>
     5 using namespace std;
     6 template <class T>  
     7 class myclass {
     8 // 在此处补充你的代码
     9 private: 
    10     T * p;
    11     int size;
    12 public:
    13     myclass(T * temp, int nn):size(nn){
    14         p = new T[nn];
    15         for(int i = 0; i < nn; i++)
    16             p[i] =  temp[i];
    17     }
    18 ~myclass( ) {
    19         delete [] p; //需要用深拷贝,否则就给删了 
    20     }
    21     void Show()
    22     {
    23         for( int i = 0;i < size;i ++ ) {
    24             cout << p[i] << ",";
    25         }
    26         cout << endl;
    27     }
    28 };
    29 int a[100];
    30 int main() {
    31     char line[100];
    32     while( cin >> line ) {
    33         myclass<char> obj(line,strlen(line));;
    34         obj.Show();
    35         int n;
    36         cin >> n;
    37         for(int i = 0;i < n; ++i)
    38             cin >> a[i];
    39         myclass<int> obj2(a,n);
    40         obj2.Show();
    41     }
    42     return 0;
    43 }

    6.排序,又见排序

     1 //自己编写一个能对任何类型的数组进行排序的mysort函数模版。只能写一个mysort模板,不能写mysort函数!
     2 #include <iostream>
     3 using namespace std; 
     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 // 在此处补充你的代码
    18 template <class T1,class T2>
    19 void mysort(T1 begin, T1 end, T2 op){
    20     for(int i = 0; i < end-begin; i++){
    21         for(int j = 0; j < end-begin-1; j++){
    22             if(op(begin[j],begin[j+1])==false)
    23                 swap(begin[j], begin[j+1]);
    24         }
    25     }
    26 } 
    27 
    28 #define NUM 5
    29 int main()
    30 {
    31     int an[NUM] = { 8,123,11,10,4 };
    32     mysort(an,an+NUM,Greater1); //从小到大排序 
    33     for( int i = 0;i < NUM; i ++ )
    34        cout << an[i] << ",";
    35     mysort(an,an+NUM,Greater2); //从大到小排序 
    36     cout << endl;
    37     for( int i = 0;i < NUM; i ++ )
    38         cout << an[i] << ","; 
    39     cout << endl;
    40     double d[6] = { 1.4,1.8,3.2,1.2,3.1,2.1};
    41     mysort(d+1,d+5,Greater3); //将数组从下标1到下标4从小到大排序 
    42     for( int i = 0;i < 6; i ++ )
    43          cout << d[i] << ","; 
    44     return 0;
    45 }//
  • 相关阅读:
    AJAX 基础知识
    jQuery知识点总结
    css基础应用总结
    javascript 总结
    找回密码-博客园
    centerOS 7 安装MySql
    java leetcode TreeNode类、ListNode类的实现
    iOS StatusBar状态栏文字颜色更改
    使用Jmeter压力测试工具测试
    安装node.js
  • 原文地址:https://www.cnblogs.com/fangziyuan/p/12592861.html
Copyright © 2020-2023  润新知