count_if函数里面的第三个参数的书写方式<<0926


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

class Cmp
{
    public:
        Cmp(int i)
            :date_(i)
        {
        }
        bool operator()(int &score)//operator和()之间为重载的操作符,这里重载的是()操作符,后面的一个括号是参数表,如此可理解Cmp类的编写原理
        {
            return score >= date_; 
        }
    private:
       int  date_;
};

bool cmp(int i)
{
    return i >= 60;
}

int main(int argc, const char *argv[])
{
    vector<int> vec;
    vec.push_back(11);
    vec.push_back(3);
    vec.push_back(90);
    vec.push_back(38);
    vec.push_back(70);
    vec.push_back(80);

//    int my_count = count_if(vec.begin(),vec.end(),Cmp(60));
//    int my_count = count_if(vec.begin(),vec.end(),[](int i){return i >= 60;});
      int my_count = count_if(vec.begin(),vec.end(),cmp);
      
    cout << my_count << endl;
    return 0;
}

posted on 2014-09-29 01:17  __hello world  阅读(507)  评论(0编辑  收藏  举报

导航