PrevNext

(Optional) C++ - Lambda Expressions

Author: Benjamin Qi

Defining anonymous function objects.

Introduction

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 {
2
3template<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)) {}
9
10 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 10
5}

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!

Give Us Feedback on (Optional) C++ - Lambda Expressions!

PrevNext