【Tricks】Lambda表达式

前言

很多时候,我们需要定义一些小的函数
比如在sort()时指定比较函数cmp()

sort(a+1, a+1+n, cmp)
bool cmp(const int a, cnost int b) {return a > b;}

但这么小的函数写在外面会有一点不优雅
于是我们可以用 λ表达式 来解决这个问题

解决方案

Lambda表达式如果叫做匿名函数的话可能更能凸显他的特性
详细情况请参阅参考资料中的链接
对于竞赛来讲,似乎Lambda表达式这么用就好:

static auto cmp = [](int a, int b) {return a > b;};
sort(a+1, a+1+n, cmp);

或者再做绝一点:

sort(a+1, a+1+n, [](int a, int b) {return a > b;});

是不是感觉很强 ( •̀ ω •́ )y

兼容性

无论是 Lambda表达式 或是auto都是 C++ 11里的东西
直接交到不支持 C++ 11 的OJ上会CE
于是我们需要加上下面这句指令,以使编译器忽略该错误:

#pragma GCC diagnostic error "-std=c++11"

当然gcc的版本需要在4.7以上,否则编译器根本不知道Lambda表达式是什么 QwQ

参考资料

  1. http://en.cppreference.com/w/cpp/language/lambda
  2. http://www.lellansin.com/c-lambda表达式基本用法.html

22 thoughts to “【Tricks】Lambda表达式”

  1. Please let me know if you’re looking for a article writer
    for your weblog. You have some really good articles and I
    believe I would be a good asset. If you ever want to
    take some of the load off, I’d love to write some articles for your blog in exchange for a link back to mine.
    Please send me an email if interested. Thank you!

  2. I think this is among the most important info for me. And i’m glad
    reading your article. However wanna observation on some basic issues, The site style is perfect, the articles is truly nice : D.
    Excellent job, cheers

  3. Someone essentially assist to make critically articles I might
    state. That is the first time I frequented your web page and so far?
    I surprised with the research you made to create
    this particular publish extraordinary. Fantastic activity!

  4. It is actually a great and useful piece of information. I am satisfied that you just shared this useful
    information with us. Please keep us up to date like this.
    Thank you for sharing.

  5. I don’t even know how I ended up here, but I thought this
    post was good. I do not know who you are but definitely
    you are going to a famous blogger if you are not already 😉 Cheers!

  6. Woah! I’m really loving the template/theme of this blog.

    It’s simple, yet effective. A lot of times it’s very difficult to get that
    “perfect balance” between user friendliness and visual appeal.
    I must say that you’ve done a superb job with this.
    Also, the blog loads super quick for me on Internet explorer.

    Superb Blog!

  7. I’m not sure exactly why but this blog is loading extremely slow for me. Is anyone else having this problem or is it a problem on my end? I’ll check back later and see if the problem still exists.

  8. Thanks for some other magnificent post. Where else could anyone
    get that type of info in such an ideal manner of writing?
    I’ve a presentation subsequent week, and I’m at the look for such
    information.

  9. Wonderful site. Plenty of useful info here. I’m sending it to
    some pals ans additionally sharing in delicious. And obviously, thank you on your sweat!

  10. Yesterday, while I was at work, my sister stole my iPad and tested to see if it can survive a 40 foot drop,
    just so she can be a youtube sensation. My apple ipad is now destroyed
    and she has 83 views. I know this is completely off topic but I had to share
    it with someone!

  11. Hey There. I discovered your blog the use of msn. This is a really well written article.

    I’ll be sure to bookmark it and return to learn extra of
    your helpful information. Thanks for the post.
    I will definitely comeback.

  12. of course like your web-site but you need to check the spelling on several
    of your posts. Several of them are rife with spelling
    problems and I find it very troublesome to inform the reality on the other hand I’ll certainly come back
    again.

  13. Hey There. I found your blog using msn. This is an extremely
    well written article. I will be sure to bookmark it and return to read more of your useful info.
    Thanks for the post. I will certainly comeback.

Leave a Reply

Your email address will not be published. Required fields are marked *