Skip to content

Instantly share code, notes, and snippets.

Created May 21, 2014 16:35
Show Gist options
  • Select an option

  • Save anonymous/719a51c35898f8b866ce to your computer and use it in GitHub Desktop.

Select an option

Save anonymous/719a51c35898f8b866ce to your computer and use it in GitHub Desktop.

Revisions

  1. @invalid-email-address Anonymous created this gist May 21, 2014.
    19 changes: 19 additions & 0 deletions capture.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    #include <iostream>
    #include <algorithm>
    #include <vector>
    using namespace std;

    int main()
    {
    // The user would introduce different values for divisor
    int divisor = 3;
    vector<int> numbers { 1, 2, 3, 4, 5, 10, 15, 20, 25, 35, 45, 50 };
    for_each(numbers.begin(), numbers.end(),
    [divisor] (int y)
    {
    if (y % divisor == 0)
    {
    cout << y << endl;
    }
    });
    }
    11 changes: 11 additions & 0 deletions ex1.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    #include <iostream>
    using namespace std;

    int main()
    {
    auto lambda = [](void) -> void
    {
    cout << "Code within a lambda expression" << endl;
    };
    lambda();
    }
    9 changes: 9 additions & 0 deletions ex2.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    #include <iostream>
    using namespace std;

    int main()
    {
    auto sum = [](int x, int y) { return x + y; };
    cout << sum(5, 2) << endl;
    cout << sum(10, 5) << endl;
    }
    19 changes: 19 additions & 0 deletions ex3.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    #include <iostream>
    #include <algorithm>
    #include <vector>
    using namespace std;

    bool is_greater_than_5(int value)
    {
    return (value > 5);
    }

    int main()
    {
    vector<int> numbers { 1, 2, 3, 4, 5, 10, 15, 20, 25, 35, 45, 50 };
    auto greater_than_5_count = count_if
    (numbers.begin(), numbers.end(),
    [](int x) -> bool {return x > 5;});
    cout << "The number of elements greater than 5 is: "
    << greater_than_5_count << "." << endl;
    }
    27 changes: 27 additions & 0 deletions ex4.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    #include <iostream>
    #include <algorithm>
    #include <vector>
    using namespace std;

    void printnumber(int y)
    {
    cout << y << endl;
    }

    struct PrintNumStruct
    {
    void operator() (int y)
    {
    cout << y << endl;
    }
    } printnumberobject;

    int main()
    {
    vector<int> numbers { 1, 2, 3, 4, 5, 10, 15, 20, 25, 35, 45, 50 };
    for_each(numbers.begin(), numbers.end(),
    [](int y) -> void
    {
    cout << y << endl;
    });
    }
    1 change: 1 addition & 0 deletions readme.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    http://www.drdobbs.com/cpp/lambdas-in-c11/240168241
    28 changes: 28 additions & 0 deletions std_function.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    #include <iostream>
    #include <algorithm>
    #include <vector>
    #include <functional>
    using namespace std;

    void run_within_for_each(std::function<void (int)> func)
    {
    vector<int> numbers{ 1, 2, 3, 4, 5, 10, 15, 20, 25, 35, 45, 50 };

    for_each(numbers.begin(), numbers.end(), func);
    }

    int main()
    {
    auto func1 = [](int y)
    {
    cout << y << endl;
    };

    auto func2 = [](int z)
    {
    cout << z * 2 << endl;
    };

    run_within_for_each(func1);
    run_within_for_each(func2);
    }