## DEV Community is a community of 697,485 amazing developers

We're a place where coders share, stay up-to-date and grow their careers. # C++ Productivity Hacks Jeevan Chandra Joshi
MCA Student at College of Technology, GBPUA&T Pantnagar, Campus Ambassador, Competitive Programmer, Open Source Contributor

``````#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;

## Discussion (2) Sandor Dargo

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 that `std::accumulate` is part of the `<numeric>` header.

A few comments without wanting to be complete here.

1. 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.

6.-8. In C++ there is no built-in `__builtin_popcount` nor `__gcd`. Some compilers might or might not provide them. But at least `std::gcd` is provided by the the standard library in `<numeric>` header. 