zoukankan      html  css  js  c++  java
  • Lambda Function.

    1. Definition:

    [capture] (parameters) mutable exception attribute -> return_type { body }

    功能上约等于:
    a.函数对象(仿函数), 仿函数可存储范围更广的数据
    b.局部函数

    http://zh.wikipedia.org/wiki/%E5%8C%BF%E5%90%8D%E5%87%BD%E6%95%B0#C.2B.2B_11

    2. Benifit:

        a. 可读性好

        b. 捕捉上下文数据

     3. Sample:

    #include "stdafx.h"
    #include <algorithm>
    #include <iostream>
    #include <vector>
    using namespace std;
    
    class _F
    {
        public:
            void operator()(int d) const
            { 
                cout << d << " "; 
            }
    
    };
    
    void Print(int n) 
    { 
        cout << n << " "; 
    }
    
    class _PreStatusF
    {
    public:
        _PreStatusF(int n):d(n){}
        void operator()() const
        { 
            cout << d << " "; 
        }
    private:
        int d;
    
    };
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        vector<int> v;
    
        for (int i = 0; i < 10; ++i) 
            v.push_back(i);
    
        for_each(v.begin(), v.end(), [](int n) { cout << n << " "; });//1. lambda function
        cout <<endl;
    
        for_each(v.begin(), v.end(), Print);//2. function point
        cout <<endl;
    
        for_each(v.begin(), v.end(), _F()); //3. function object
        cout <<endl;
        
        _PreStatusF test(100); //function object can record the precondition.
        test();
    
    
        return 0;
    }

    Result:

  • 相关阅读:
    模板-树链剖分
    bzoj2523 聪明的学生
    P1220 关路灯
    BZOJ3572 [Hnoi2014]世界树
    BZOJ4013 [HNOI2015]实验比较
    BZOJ4012 [HNOI2015]开店
    BZOJ4011 [HNOI2015]落忆枫音
    BZOJ4009 [HNOI2015]接水果
    BZOJ4010 [HNOI2015]菜肴制作
    BZOJ4008 [HNOI2015]亚瑟王
  • 原文地址:https://www.cnblogs.com/anit/p/3896954.html
Copyright © 2011-2022 走看看