将函数对象用于标准库算法

 1 #include<iostream>
 2 #include<vector>
 3 
 4 bool GT6(const int &s){
 5     return s>=6;
 6     }
 7 int main(){
 8     int a[]={0,1,2,3,4,5,6,7,8,9};
 9     std::vector<int>vec(a,a+10);
10     std::cout<<count_if(vec.begin(),vec.end(),GT6)<<std::endl;
11    return 0;
12 }
#include<iostream>
#include<vector>

class GT_cls{
public:
    GT_cls(int val=0):i(val){}
    bool operator()(const int &s){
        return s>=i;
    }
private:
    int i;
};

int main(){
    int a[]={0,1,2,3,4,5,6,7,8,9};
    std::vector<int>vec(a,a+10);
    std::cout<<count_if(vec.begin(),vec.end(),GT_cls(6) )<<std::endl;
   return 0;
}

 

posted @ 2015-01-21 02:08  vertigo  阅读(98)  评论(0)    收藏  举报