1. <tfoot id='arh5y'></tfoot>
    1. <legend id='arh5y'><style id='arh5y'><dir id='arh5y'><q id='arh5y'></q></dir></style></legend>

      <i id='arh5y'><tr id='arh5y'><dt id='arh5y'><q id='arh5y'><span id='arh5y'><b id='arh5y'><form id='arh5y'><ins id='arh5y'></ins><ul id='arh5y'></ul><sub id='arh5y'></sub></form><legend id='arh5y'></legend><bdo id='arh5y'><pre id='arh5y'><center id='arh5y'></center></pre></bdo></b><th id='arh5y'></th></span></q></dt></tr></i><div id='arh5y'><tfoot id='arh5y'></tfoot><dl id='arh5y'><fieldset id='arh5y'></fieldset></dl></div>

      • <bdo id='arh5y'></bdo><ul id='arh5y'></ul>

      <small id='arh5y'></small><noframes id='arh5y'>

    2. C++ 中的 Lambda 表达式与函子

      时间:2023-06-30

      1. <small id='tKr0m'></small><noframes id='tKr0m'>

          <bdo id='tKr0m'></bdo><ul id='tKr0m'></ul>
            <tfoot id='tKr0m'></tfoot>
            <i id='tKr0m'><tr id='tKr0m'><dt id='tKr0m'><q id='tKr0m'><span id='tKr0m'><b id='tKr0m'><form id='tKr0m'><ins id='tKr0m'></ins><ul id='tKr0m'></ul><sub id='tKr0m'></sub></form><legend id='tKr0m'></legend><bdo id='tKr0m'><pre id='tKr0m'><center id='tKr0m'></center></pre></bdo></b><th id='tKr0m'></th></span></q></dt></tr></i><div id='tKr0m'><tfoot id='tKr0m'></tfoot><dl id='tKr0m'><fieldset id='tKr0m'></fieldset></dl></div>

                  <tbody id='tKr0m'></tbody>
                <legend id='tKr0m'><style id='tKr0m'><dir id='tKr0m'><q id='tKr0m'></q></dir></style></legend>
                本文介绍了C++ 中的 Lambda 表达式与函子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我想知道在 C++ 中我们应该在哪里使用 lambda 表达式而不是函子.对我来说,这两种技术基本相同,甚至 functor 也比 lambda 更优雅、更简洁.例如,如果我想重用我的谓词,我必须一遍又一遍地复制 lambda 部分.那么 lambda 什么时候真正到位?

                I wonder where should we use lambda expression over functor in C++. To me, these two techniques are basically the same, even functor is more elegant and cleaner than lambda. For example, if I want to reuse my predicate, I have to copy the lambda part over and over. So when does lambda really come in to place?

                推荐答案

                1) 它是微不足道的,试图分享它更多的是工作而不是利益.

                1) It's trivial and trying to share it is more work than benefit.

                2) 定义函子只会增加复杂性(因为必须创建一堆成员变量和废话).

                2) Defining a functor simply adds complexity (due to having to make a bunch of member variables and crap).

                如果这些都不是真的,那么也许你应该考虑定义一个函子.

                If neither of those things is true then maybe you should think about defining a functor.

                您似乎需要一个示例,说明何时在函子上使用 lambda 会更好.给你:

                it seems to be that you need an example of when it would be nice to use a lambda over a functor. Here you go:

                typedef std::vector< std::pair<int,std::string> > whatsit_t;
                
                int find_it(std::string value, whatsit_t const& stuff)
                {
                  auto fit = std::find_if(stuff.begin(), stuff.end(), [value](whatsit_t::value_type const& vt) -> bool { return vt.second == value; });
                
                  if (fit == stuff.end()) throw std::wtf_error();
                
                  return fit->first;
                }
                

                如果没有 lambdas,您将不得不使用类似的东西在现场构造一个函子,或者为一些烦人的琐碎事情编写一个外部可链接的函子对象.

                Without lambdas you'd have to use something that similarly constructs a functor on the spot or write an externally linkable functor object for something that's annoyingly trivial.

                顺便说一句,我想也许 wtf_error 是一个扩展.

                BTW, I think maybe wtf_error is an extension.

                这篇关于C++ 中的 Lambda 表达式与函子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:lambda 函数对象中的静态变量如何工作? 下一篇:在 lambda 函数语法中,“捕获列表"有什么用途?

                相关文章

                • <bdo id='SHMDo'></bdo><ul id='SHMDo'></ul>
                <i id='SHMDo'><tr id='SHMDo'><dt id='SHMDo'><q id='SHMDo'><span id='SHMDo'><b id='SHMDo'><form id='SHMDo'><ins id='SHMDo'></ins><ul id='SHMDo'></ul><sub id='SHMDo'></sub></form><legend id='SHMDo'></legend><bdo id='SHMDo'><pre id='SHMDo'><center id='SHMDo'></center></pre></bdo></b><th id='SHMDo'></th></span></q></dt></tr></i><div id='SHMDo'><tfoot id='SHMDo'></tfoot><dl id='SHMDo'><fieldset id='SHMDo'></fieldset></dl></div>

                  <tfoot id='SHMDo'></tfoot>
                1. <small id='SHMDo'></small><noframes id='SHMDo'>

                  <legend id='SHMDo'><style id='SHMDo'><dir id='SHMDo'><q id='SHMDo'></q></dir></style></legend>