Created
May 21, 2014 16:35
-
-
Save anonymous/719a51c35898f8b866ce to your computer and use it in GitHub Desktop.
Revisions
-
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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; } }); } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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(); } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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; } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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; } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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; }); } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1 @@ http://www.drdobbs.com/cpp/lambdas-in-c11/240168241 This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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); }