码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2015-01-21 06:34:57      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:

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

 

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

标签:

原文地址:http://www.cnblogs.com/vertigo/p/4237839.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!