Skip to content

Instantly share code, notes, and snippets.

@kodekracker
Last active December 7, 2024 13:53
Show Gist options
  • Save kodekracker/e09f9d23573f117a5db0 to your computer and use it in GitHub Desktop.
Save kodekracker/e09f9d23573f117a5db0 to your computer and use it in GitHub Desktop.
Basic C++ Template for Competitive Programming
/*
* Note: This template uses some c++11 functions , so you have to compile it with c++11 flag.
* Example:- $ g++ -std=c++11 c++Template.cpp
*
* Author : Akshay Pratap Singh
* Handle: code_crack_01
*
*/
/******** All Required Header Files ********/
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <queue>
#include <deque>
#include <bitset>
#include <iterator>
#include <list>
#include <stack>
#include <map>
#include <set>
#include <functional>
#include <numeric>
#include <utility>
#include <limits>
#include <time.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
using namespace std;
/******* All Required define Pre-Processors and typedef Constants *******/
#define SCD(t) scanf("%d",&t)
#define SCLD(t) scanf("%ld",&t)
#define SCLLD(t) scanf("%lld",&t)
#define SCC(t) scanf("%c",&t)
#define SCS(t) scanf("%s",t)
#define SCF(t) scanf("%f",&t)
#define SCLF(t) scanf("%lf",&t)
#define MEM(a, b) memset(a, (b), sizeof(a))
#define FOR(i, j, k, in) for (int i=j ; i<k ; i+=in)
#define RFOR(i, j, k, in) for (int i=j ; i>=k ; i-=in)
#define REP(i, j) FOR(i, 0, j, 1)
#define RREP(i, j) RFOR(i, j, 0, 1)
#define all(cont) cont.begin(), cont.end()
#define rall(cont) cont.end(), cont.begin()
#define FOREACH(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define IN(A, B, C) assert( B <= A && A <= C)
#define MP make_pair
#define PB push_back
#define INF (int)1e9
#define EPS 1e-9
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
#define read(type) readInt<type>()
const double pi=acos(-1.0);
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<PII> VII;
typedef vector<VI> VVI;
typedef map<int,int> MPII;
typedef set<int> SETI;
typedef multiset<int> MSETI;
typedef long int int32;
typedef unsigned long int uint32;
typedef long long int int64;
typedef unsigned long long int uint64;
/****** Template of some basic operations *****/
template<typename T, typename U> inline void amin(T &x, U y) { if(y < x) x = y; }
template<typename T, typename U> inline void amax(T &x, U y) { if(x < y) x = y; }
/**********************************************/
/****** Template of Fast I/O Methods *********/
template <typename T> inline void write(T x)
{
int i = 20;
char buf[21];
// buf[10] = 0;
buf[20] = '\n';
do
{
buf[--i] = x % 10 + '0';
x/= 10;
}while(x);
do
{
putchar(buf[i]);
} while (buf[i++] != '\n');
}
template <typename T> inline T readInt()
{
T n=0,s=1;
char p=getchar();
if(p=='-')
s=-1;
while((p<'0'||p>'9')&&p!=EOF&&p!='-')
p=getchar();
if(p=='-')
s=-1,p=getchar();
while(p>='0'&&p<='9') {
n = (n<< 3) + (n<< 1) + (p - '0');
p=getchar();
}
return n*s;
}
/************************************/
/******* Debugging Class Template *******/
#define DEBUG
#ifdef DEBUG
#define debug(args...) (Debugger()) , args
class Debugger
{
public:
Debugger(const std::string& _separator = " - ") :
first(true), separator(_separator){}
template<typename ObjectType> Debugger& operator , (const ObjectType& v)
{
if(!first)
std:cerr << separator;
std::cerr << v;
first = false;
return *this;
}
~Debugger() { std:cerr << endl;}
private:
bool first;
std::string separator;
};
#else
#define debug(args...) // Just strip off all debug tokens
#endif
/**************************************/
/******** User-defined Function *******/
/**************************************/
/********** Main() function **********/
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
#endif
int tc;
tc = read(int);
while(tc--){
write(tc);
}
return 0;
}
/******** Main() Ends Here *************/
@CraigglesO
Copy link

This is awesome. I am surprised you don't have "INT_MAX" in here.

@pratyush247
Copy link

Can you tell me how to use it ?

@MohitVachhani
Copy link

Just make a variable and declare it to INT_MAX and then you are good to go.
Example-
int max1=INT_MAX;

@brodzik
Copy link

brodzik commented Sep 10, 2017

#define INF (int)1e9 is used instead of INT_MAX so that operations like INF + INF are possible and don't become negative (out of range).

@92ganesh
Copy link

rather than including tons of header files
use # include <bits/stdc++.h>
it has all necessary basic header files for CP

@onlined
Copy link

onlined commented Feb 10, 2018

bits/stdc++.h is not standard.

@sourabh2k15
Copy link

yup bits/stdc++ header is not found on all platforms

@t41y0u
Copy link

t41y0u commented Oct 20, 2018

bits/stdc++.h also takes more time to compile

Copy link

ghost commented Aug 29, 2019

bits/stdc++.h works on almost every online judge afaik, even those like tourist use bits/stdc++.h (just check out submissions from top rankings of CodeForces), although some like rng_58 still decides to have a bunch of includes

@rahul-sangvikar
Copy link

@Hikarii0703 , it doesn't work on OS X

@darkrairsk
Copy link

/******* Debugging Class Template *******/
#define DEBUG

#ifdef DEBUG

#define debug(args...)     (Debugger()) , args

class Debugger
{
    public:
    Debugger(const std::string& _separator = " - ") :
    first(true), separator(_separator){}

    template<typename ObjectType> Debugger& operator , (const ObjectType& v)
    {
        if(!first)
            std:cerr << separator;
        std::cerr << v;
        first = false;
        return *this;
    }
    ~Debugger() {  std:cerr << endl;}

    private:
    bool first;
    std::string separator;
};

#else
#define debug(args...) // Just strip off all debug tokens
#endif

i really didn't understand this part!

Copy link

ghost commented Jan 23, 2021

the write function can only write, positive numbers and not negative :-(
Other wise the template is pretty good.

@adityaraj5200
Copy link

adityaraj5200 commented Feb 11, 2021

I am getting this error. Can any1 help?
I am using VS Code.

error: 'vector' does not name a type; did you mean 'wctob'?
typedef vector VI;
^~~~~~
wctob

Also ,Let me explain a bit more, I am using these typedefs:

typedef long long ll;
typedef long double ld;
typedef pair<int, int> PII;
typedef vector VI;
typedef vector VS;
typedef vector VII;
typedef vector VVI;

it's throwing above type of error for every other typedef except the first two ones i.e typedef long long ll;, typedef long double ld;

@prjaga
Copy link

prjaga commented Mar 5, 2021

it's throwing above type of error for every other typedef except the first two ones i.e typedef long long ll;, typedef long double ld;

You need to define contents of vector like vector<int>, vector<string>, etc as compiler doesn't know what type of vector you are talking about.

@yashjain0
Copy link

You can use header #include <bits/stdc++.h> it contains all the standard libraries of C++.

@ahmadsb86
Copy link

handsome

@Krish2428
Copy link

How to load the template each time you run or create any new file in vscode ?

@marcoshernanz
Copy link

You can write a code snippet and use it every time you create a file for CP

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment