程序设计与算法(三)第七周 作业

本文通过多个实例介绍了C++模板编程的应用,包括数组求和、foreach遍历、过滤元素、自定义输入迭代器、模板类实现及排序算法等。这些示例展示了如何使用模板来提高代码的通用性和复用性。

027:简单的SumArray

#include <iostream>
#include <string>
using namespace std;
template <class T>
T SumArray(T *a, T *b){
    T sum=*a;
    for (int i = 1; i < b-a; ++i) {
        sum+=*(a+i);
    }
    return sum;
}
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

028:简单的foreach

#include <iostream>
#include <string>
using namespace std;

template <class T, class pred>
void MyForeach(T *a, T *b, pred op){
    for(;a!=b;a++){
        op(*a);
    }
}

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;
}
// input:
//3 4
//Tom Mike Jack
//1 2 3 4
//1 2
//Peking
//100 200

// output:
//TomMikeJack
//2,3,4,5,
//Peking
//101,201,

029:简单的Filter

#include <iostream>
#include <string>
using namespace std;

template <class T, class oper>
T *Filter(T *a, T *b, T *c, oper op)
{
    for (; a!=b;a++) {
        if(op(*a)){
            *c=*a;
            c++;
        }
    }
    return c;
};

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

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

#include <iostream>
using namespace std;

class MyCin
{
private:
    bool state= true;
public:
    MyCin &operator >> (int &x){
        int t;
        cin>>t;
        x = t;
        if(x==-1) {
            state=false;
        }
        return *this;
    }
    operator bool(){
//  类型转换构造函数,可以将对象转换为一个指定类型的数据
        return state;
    }
};
int main()
{
    MyCin m;
    int n1,n2;
    while( m >> n1 >> n2)
        cout  << n1 << " " << n2 << endl;
    return 0;
}

031:山寨版istream_iterator

#include <iostream>
#include <string>

using namespace std;
template <class T>
class CMyistream_iterator
{
private:
    T t;
    istream &s;
public:
    CMyistream_iterator(istream &is):s(is){
        s>>t;
    };
    T operator *(){
    //重载*运算符
            return t;
    };
    void operator ++(int){
        //后置++要加一个参数表示
        s>>t;
    }
};



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<<"there"<<endl;
        cout << n1 << " " << n2<< " " << n3 << " ";
        CMyistream_iterator<string> inputStr(cin);
        string s1,s2;
        s1 = * inputStr;
        inputStr ++;
        s2 = * inputStr;
        cout << s1 << " " << s2 << endl;
    }
    return 0;
}

032:这个模板并不难

#include <iostream>
#include <string>
#include <cstring>
using namespace std;
template <class T>  
class myclass {

private:
    size_t size;
    T *p;
public:
    myclass(T *a, size_t _size){
        size = _size;
        p = new T[size];//析够函数用delete因此,这里要用new初始化数组
        for (int i = 0; i < size; ++i) {
            *(p+i) = a[i];
        }
    }

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

033:排序,又见排序!

#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(T1* t1, T1* t2, T2 op){
    //这里写个排序
    for (int i = 0; i < t2-t1; ++i) {
        for (int j = i+1; j < t2-t1; ++j) {
            if(op(*(t1+j), *(t1+i))){
                T1 temp = *(t1+i);
                *(t1+i)=*(t1+j);
                *(t1+j)=temp;
            }
        }
    }
};

#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;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值