Simple C++ Competitive Programming Template

rajat aggarwal
Geek Culture
Published in
2 min readSep 8, 2020

--

I have been searching for a standard redundant piece of code that I have to write in each of my C++ programs. While searching I found many, some of them were really good but they had many unnecessary template classes and too many functions which I thought were not of use much.

So after going through them all I created a template for myself. And I am sharing this hoping it would be beneficial to others :

#include <bits/stdc++.h>using namespace std;#define read(type) readInt<type>() // Fast read#define ll long long#define nL "\n"#define pb push_back#define mk make_pair#define pii pair<int, int>#define a first#define b second#define vi vector<int>#define all(x) (x).begin(), (x).end()#define umap unordered_map#define uset unordered_set#define MOD 1000000007#define imax INT_MAX#define imin INT_MIN#define exp 1e9#define sz(x) (int((x).size()))int32_t main(){    ios_base::sync_with_stdio(false);    cin.tie(NULL);    int ttt; cin >> ttt;    while(ttt--) {    }    return 0;}

The code will run for the given sample test cases

I have used ‘ int32_t main() ’ instead of ‘ int main() ’ as if you have written the program for using “ int ” variables but later on you realize you have to increase the range with ‘long long’.

So intead of,
#define ll long long
use :
#define int long long

This would save you the time to change each and every int variables, but there is no ‘ long long ’ main function so it is better to just change it to

int32_t main()
instead of : int main()

I have used ‘ios_base::sync_with_stdio(false);’ this snippet to increase the execution speed of cin and cout. I have explained the same in this blog

All the rest #define are just abbreviations of the most used functions in any program.

--

--

rajat aggarwal
Geek Culture

Software Engineer , hope you’ll appreciate the content. Also follow me on vocal @ https://vocal.media/authors/rajat-ir2e1e0bfm