对c++编程方式的一点思考-读muduo有感。

现在脑子有点乱,一时间不知道怎么准备语言,就先把代码放到这,以后再补上。

#include <bits/stdc++.h>
#include <functional>
using namespace std;
using namespace std::placeholders;


class Foo
{
    typedef function<int ()> LenCall;
    typedef function<bool (int i , int j)> LessCall;
    typedef function<void (int i , int j)> SwapCall;
public:
    Foo(LenCall lencall , LessCall lesscall , SwapCall swapcall) :
        lencall_(lencall) , lesscall_(lesscall) , swapcall_(swapcall) {};
    void sort_t()
    {
        int len = lencall_();
        cout<<"len : "<<len<<endl;
        for(int i=0; i<len; i++)
           for(int j=i+1; j<len; j++)
           {
                if(lesscall_(i , j))
                    swapcall_(i , j);
           }
    }
private:
    SwapCall swapcall_;
    LenCall  lencall_;
    LessCall lesscall_;
};


class People
{
public:
    vector<int> arr;
    People(initializer_list<int> list)
    {
        arr.clear();
        for(auto it = list.begin(); it!=list.end(); it++)
            arr.push_back(*it);
    };
public:
    int Len()
    {
        return arr.size();
    }
    bool Less(int i , int j)
    {
        if(arr[i] > arr[j])
            return true;
        return false;
    }
    void Swap(int i , int j)
    {
        swap(arr[i] , arr[j]);
    }
    void Printf()
    {
        for(auto it = arr.begin(); it!=arr.end(); it++)
            cout<<*it<<" ";
        cout<<endl;
    }
};

class Student
{
    struct stu{
        string name;
        int age;
        int grade;
    };
public:
    vector<stu> students;
    Student(initializer_list<stu> list)
    {
        students.clear();
        for(auto it=list.begin(); it!=list.end(); it++)
        {
            students.push_back(*it);
        }
    }
public:
    int Len() {return students.size();}
    bool Less(int i , int j)
    {
        if(students[i].grade != students[j].grade)
            return students[i].grade > students[j].grade;
        if(students[i].age != students[j].age)
            return students[i].age > students[j].age;
        return students[i].name > students[j].name;
    }
    void Swap(int i , int j)
    {
        swap(students[i] , students[j]);
    }
    void Printf()
    {
        for(auto it = students.begin(); it!=students.end(); it++)
        {
            cout<<(*it).name<<" "<<(*it).age<<" "<<(*it).grade<<endl;
        }
    }
};
int main()
{
    /*People person({3 , 2 , 5 , 4 , 1 , 8 , 9});

    Foo foo(bind(&People::Len , &person) ,
            bind(&People::Less , &person , _1 , _2),
            bind(&People::Swap , &person , _1 , _2)
            );
    person.Printf();
    foo.sort_t();
    person.Printf();
    */

    Student s( {{"lianxm" , 18 , 2} , {"zhangheping" , 17 , 1} , {"limengyao" , 18 , 1}} );
    s.Printf();
    Foo foo(bind(&Student::Len , &s) ,
            bind(&Student::Less , &s , _1 , _2) ,
            bind(&Student::Swap , &s , _1 , _2)
            );

    foo.sort_t();
    cout<<endl;
    s.Printf();
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值