(Optional) C++ - Lambda Expressions
Author: Benjamin Qi
Defining anonymous function objects.
Introduction
Resources | |||
---|---|---|---|
CPP | reference | ||
UMich | |||
SO | |||
Microsoft |
Anything more beginner-friendly?
This section is not complete.
Feel free to file a request to complete this using the "Contact Us" button.
Recursive Lambdas
Resources | |||
---|---|---|---|
open-std | |||
RIP Tutorial |
If we add the following from the link above in C++14:
1namespace std {23template<class Fun>4class y_combinator_result {5 Fun fun_;6public:7 template<class T>8 explicit y_combinator_result(T &&fun): fun_(std::forward<T>(fun)) {}910 template<class ...Args>
Then we can have code like the following!
1int main() {2 cout << y_combinator([](auto gcd, int a, int b) -> int {3 return b == 0 ? a : gcd(b, a % b);4 })(20,30) << "\n"; // outputs 105}
Looks like ecnerwala uses these a lot.
Module Progress:
Join the USACO Forum!
Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!