1. Master Header File
#include <bits/stdc++.h>
2. Faster I/O Operations
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
3. Macros for Debugging
#define deb(x) cout << #x << " : " << x << "\n"
4. Number of Digits
int number = 12345;
int digits = floor(log10(number) + 1); // 5
5. Swap Two Numbers
int a = 10;
int b = 20;
swap(a, b); // 20 10
6. Number of Set Bits
int n = 15;
int setBit = __builtin_popcount(n); // 4
7. Auto Keyword
vector<int> v{1, 2, 3, 4, 5};
for (auto x : v)
cout << x << " "; // 1 2 3 4 5
8. Inbuilt GCD Function
int x = 12;
int y = 16;
int gcd = __gcd(x, y); // 4
9. Lambda Function
auto add = [](int x, int y) {
return x + y;
};
int sum = add(10, 20); // 30
10. Accumulate Function
int sum = 10;
int arr[] = {20, 30, 40};
sum = accumulate(arr, arr + 3, sum); // 100
Tip: Use "\n" instead of endl;
Top comments (2)
In my opinion it would be worth explaining why something is considered a productivity trick at what it does. Plus, prepend
std::
in case it's needed, because your readers might not know thatstd::accumulate
is part of the<numeric>
header.A few comments without wanting to be complete here.
In production environment never ever use
<bits/stdc++.h>
. It will blow the size of your software as it includes whatever, plus it's not portable between different compilers.No. In 2021 you barely need macros. Start with this.
6.-8. In C++ there is no built-in
__builtin_popcount
nor__gcd
. Some compilers might or might not provide them. But at leaststd::gcd
is provided by the the standard library in<numeric>
header.+1) Productivity trick. Understand what you use and why you use it, that's the only thing that will boost your productivity in the long run.
Please don't get me wrong. But let me be honest with you. This is not an "hack" rather the "worst possible software engineering practice". Yeah, "hacking" is an "anti-pattern" in SWE itself. Please avoid these type "magic numbers and library call". This is okay for some fancy competitive programming environment, not in Software Engineering.
Cheers!