首页 > 代码库 > 将成员函数用作可调用对象

将成员函数用作可调用对象

19.18编写一个函数,使用count_if统计在给定的vector中有多少个空string。

#include<algorithm>#include<string>#include<iostream>#include<vector>#include<functional>using namespace std;int main(){    vector<string> svec={"fhhd","fdf","","hafh","",""};    function<bool (const string&)> fcn=&string::empty;    cout<<find_if(svec.begin(),svec.end(),fcn)-svec.begin()<<endl;    cout<<find_if(svec.begin(),svec.end(),mem_fn(&string::empty))-svec.begin()<<endl;    cout<<find_if(svec.begin(),svec.end(),bind(&string::empty,std::placeholders::_1))-svec.begin()<<endl;    cout<<count_if(svec.begin(),svec.end(),fcn)<<endl;    cout<<count_if(svec.begin(),svec.end(),mem_fn(&string::empty))<<endl;    cout<<count_if(svec.begin(),svec.end(),bind(&string::empty,std::placeholders::_1))<<endl;}

 

将成员函数用作可调用对象