c++11 匿名函数[] [this] [&] [=]

本文探讨C++11中的匿名函数特性,包括[]不截取变量、[&]截取所有变量为引用、[=]拷贝变量、[=, &foo]的混合使用以及[bar]和[this]针对特定变量的处理。这些特性为C++函数对象提供了更灵活的捕获策略。" 136273378,7947617,"大厂机试解析:二叉树计算(Python, Java, C++, JS)

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C++11 匿名函数(由[&]想到的)

最近在读别人写的C++代码时发现一些很古怪的东西:

有这样的:

// Declare graph of factor types and build the factor structure std::vector<dtf::prior_t> priors; float tree_ms = ts::timing_ms([&]() { graph = InitFactorGraph(forest, priors, dags_train, sigma_unary, sigma_pw, levels_unary, levels_pw, feat_box_max, feat_radius); }); std::cout << "Decision tree induction took " << (tree_ms/1000.0) << "s" << std::endl;

也有这样的,主函数写了一堆声明定义以后就return一个这样的东西:

return processFrames([&](cv::Mat frame) { cv::Mat withoutBackground = bgHider.process(frame); cv::Mat eroded; cv::erode(withoutBackground, eroded, cv::Mat(), cv::Point(-1,-1), 3); cv::Mat dilated; cv::dilate(eroded, dilated, cv::Mat(), cv::Point(-1,-1), 5); //cv::Mat processed = redFilter.filter(withoutBackground); cv::Mat* showImage = 0; switch (showStage) { case 0: showImage = &frame; break; case 1: showImage = &withoutBackground; break; case 2: showImage = &eroded; break; case 3: showImage = &dilated; break; default: showImage = 0; } if (showImage) { std::ostringstream oss; auto millis = stopWatch.getMillisAndReset(); oss << 1000/millis; cv::putText(*showImage, oss.str(),cv::Point(30,frame.rows-30), cv::FONT_HERSHEY_COMPLEX_SMALL, 0.8,

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值