Lambda Expressions in C++

Lambda expressions are anonymous unnamed function objects.

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{
    int i = 30;
    int x = 50;
    auto lambda = [i, x]()
    { cout << "Sum is : " << i + x; };
    lambda();
    auto l = [](int a, int b)
    {
        cout << endl
             << "The value of a: " << a << " and b: " << b << endl;
    };
    l(60, 7);

    vector<int> v = {2, 6, 8, 7, 4, 20, 19, 78, 54, 44};
    auto count = count_if(begin(v), end(v), [](int i)
                          { return i % 2 == 0; });
    cout << "Even Count : " << count << endl;

    return 0;
}

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

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