函数对象:多态性,暂态表达式,
类名+操作符号sort(coll.begin(),coll.end(),cmp());
cmp()是一个暂态表达式;
函数对象的多态性依靠类的属性,及通过传递不同的参数到类构造函数之中;生成函数对象的不同状态的实例。
class str {
public:
string a;
string b;
str(string a1, string b1) : a(a1), b(b1) {};
};
class cmp {
public:
bool operator() (const str&a1,const str &b1)
{
return a1.b>b1.b;
}
};
vector<str> coll;
int main() {
ios::sync_with_stdio(false);
//cin.tie(0);
//cout.tie(0);
str s1("afsdf","56asdf");
str s2("afsd56f","#$5we56asdf");
str s3("aZ&*fsdf","56fads)_*_fasdf");
coll.push_back(s1);
coll.push_back(s2);
coll.push_back(s3);
sort(coll.begin(),coll.end(),cmp());
//cmp() 是暂态表达式;
for(auto i:coll)
{
cout<<i.a<<'\t'<<i.b<<endl;
}
cout<<('5'>'#');
return 0;
}