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

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

 

上一篇:【错误记录】Android NDK 错误排查记录 ( Could not get version from cmake.dir path ‘xxx\cmake\3.6.4111459‘. )


下一篇:前端基础进阶2