(Optional) Additional C++ Tips

Author: Benjamin Qi

Lambda Expressions and Vectorization.

Lambda Expressions


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

RIP Tutorial

If we add the following from the link above in C++14:

1namespace std {
3template<class Fun>
4class y_combinator_result {
5 Fun fun_;
7 template<class T>
8 explicit y_combinator_result(T &&fun): fun_(std::forward<T>(fun)) {}
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

Looks like ecnerwala uses these a lot.


Pragmas provide additional information to the compiler. Sometimes you'll see the following lines at the beginning of a program.

1#pragma GCC optimize ("Ofast")
2#pragma GCC target ("avx2")


According to KACTL:

  • #pragma GCC optimize ("Ofast") will make GCC auto-vectorize for loops and optimizes floating points better (assumes associativity and turns off denormals).

  • #pragma GCC target ("avx,avx2") can double performance of vectorized code, but causes crashes on old machines.

According to CodinGame,

Modern CPUs can execute up to four instructions at the same time if they are independent.

Can also check Wikipedia articles about SSE and AVX (AVX is the more advanced version).

Examples from CF

The occasional Div 1 E is trivialized by this!

Where Can I Use These?

Whether these pragmas are supported depends on the computer architecture (see here).

  • Ofast with avx2 works on CF and DMOJ.
  • sse4 and avx cause runtime errors on InfoArena and Szkopu┼é. However, #pragma GCC optimize("unroll-loops") seems to work on InfoArena (compare TLE and AC).
  • I don't have reason to believe that these work for USACO.

Why Should I Not Use These?

From this comment:

To everyone who doesn't know what's going on here: seems that topicstarter doesn't know it either, and it looks like some magic for him.

It's not a good idea to include these pragmas at the start of every program. As mentioned above, these pragmas cause RE on some sites. Other times, they are just ignored by the compiler, or even worse, they might make your code slower instead of faster (ex. see here).

Module Progress:

Give Us Feedback on (Optional) Additional C++ Tips!