现在脑子有点乱,一时间不知道怎么准备语言,就先把代码放到这,以后再补上。
#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();
}