-
-
Save timmyshen/2a7950abbbbca0f7276a to your computer and use it in GitHub Desktop.
This file contains 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 characters
http://www.drdobbs.com/cpp/lambdas-in-c11/240168241 |
This file contains 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 characters
#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 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 characters
#include <iostream> | |
using namespace std; | |
int main() | |
{ | |
auto lambda = [](void) -> void | |
{ | |
cout << "Code within a lambda expression" << endl; | |
}; | |
lambda(); | |
} |
This file contains 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 characters
#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 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 characters
#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 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 characters
#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 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 characters
#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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment