c++11 匿名函数在find_if中的使用

#include <vector>
#include <algorithm>
#include <functional>

int main(int argc, char** argv) {
    int ret = 0;

    vector< int> db_{10, 20, 30, 50};

    auto func_ = [](vector<int> & v_, int32_t value_){ 
        auto it = std::find(v_.begin(), v_.end(), value_);
        cout<<"func_ ";
        if(v_.end() != it)
        {
            cout<<"find: "<<value_<<endl;
        }else
        {
            cout<<"not find: "<<value_<<endl;
        }
    };
    func_(db_, 20);
    func_(db_, 30);
    func_(db_, 40);

    auto func_1 = [](vector<int> & v_, int32_t value_){ 
        auto it = std::find_if(v_.begin(), v_.end(), [value_](const int& loop){return loop/10==(value_/10);});
        cout<<"func_1 ";
        if(v_.end() != it)
        {
            cout<<"find suc: "<<value_<<endl;
        }else
        {
            cout<<"find failed: "<<value_<<endl;
        }
    };
    func_1(db_, 22);
    func_1(db_, 33);
    func_1(db_, 44);

    return 0;
}

func_ find: 20
func_ find: 30
func_ not find: 40
func_1 find suc: 22
func_1 find suc: 33
func_1 find failed: 44

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值