Use of C++ code template

Why most people save a C++ code template where they include almost all the header file, no matter what the problem is, there code always have the same code, typical example in C++ 4.3.2

#include <iostream>
#include <cmath>
#include <algorithm>
#include <limits>
#include <vector>
#include <bitset>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <time.h>
using namespace std;
#define MOD 1000000007LL
#define LL long long
#define ULL unsigned long long
#define LD long double
#define MAX(a,b) ((a)>(b)?(a):(b))
#define MIN(a,b) ((a)<(b)?(a):(b))
#define ABS(x) ((x)<0?-(x):(x))
#define si(n) scanf("%d",&n)
#define sf(n) scanf("%f",&n)
#define sl(n) scanf("%lld",&n)
#define slu(n) scanf("%llu",&n)
#define sd(n) scanf("%lf",&n)
#define ss(n) scanf("%s",n)
#define pnl printf("\n")
#define REP(i,n) for(int i=0;i<(n);i++)
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define FORR(i,n) for(int i=(n);i>=0;i--)
#define DB(x) cout<<"\n"<<#x<<" = "<<(x)<<"\n";
#define CL(a,b) memset(a,b,sizeof(a))
#define GOLD ((1+sqrt(5))/2)
const double PI=3.14159265358979323846264338327950288419716939937510582097494459230;
void swaps (char *x,char *y){char temp;temp=*x;*x=*y;*y=temp;}
void swapi(int *a,int *b){int temp;temp=*a;*a=*b;*b=temp;}
ULL gcd(ULL a,ULL b){if(a==0)return b;if(b==0)return a;if(a==1||b==1)return 1;
if(a==b)return a;if(a>b)return gcd(b,a%b);else return gcd(a,b%a);}
#define SIZE 1000001
void preprocess()
}//end prepreprocess
void refresh()
}//end refresh
void compute()
}//end compute
int main()
    #ifdef debug
    freopen("log.txt", "w", stderr);
    int t,i,j;preprocess();
    }//end while
    #ifdef debug
    fprintf(stdout,"\nTIME: %.3lf sec\n", (double)clock()/(CLOCKS_PER_SEC));
    return 0;
}//end main*

this kind of template is quite common among online coder. So please tell me it’s use and also the use of freopen(“input.txt”,“r”,stdin);freopen(“output.txt”,“w”,stdout);freopen(“log.txt”, “w”, stderr); and the use of memset(a,b,sizeof(a))

Prakhar Avasthi

1 Like

Usage is simple, in speed programming you have no time to write includes and other often used code, so some coders create templates they use in contests…

Read the documentation for freopen and memset if it’s not clear, be welcome to ask question in comments :wink:

There was also a question about memset in forum, maybe it helps too :wink:


Due to lack of time in competitive programming,they have almost no time to type these header they copy-paste it(I think so).


1 Like

In good programming contest all that matters is logic and typing speed…that’s why tourist never uses c++ code template but still secures first position…


I think using a template is good as long as you are familiar with all the stuff and you aren’t merely copying because a lot of people have that.
Also it may be possible that other person maybe using the same template, which may increase the % matching in plagiarism checks. :stuck_out_tongue:

1 Like

Templates are basically used by programmers to increase speed of programming by reducing code to type mainly during an online contest.

It save alot of time as you don’t waste time in writing includes or complete for loop etc statements.

These templates are really very helpful during [codeforces][1] regular contest (DIV2/DIV1).

freopen() is used when you are given some input files and you have to generate ouput file like in contests [Google Codejam][2]

You can try some problems in codejam archives.

In case you have any doubt about C++ syntax you can check the official documentation

Here [Docs_cpp][3]

Note :
It is not necessary to use template to for best performance , it really depends upon your typing speed.
If you can type faster then you don’t need it.
Also,templates have portability issues , i.e you have to carry it with you if you change your system or write it again.

Sometimes, using templates for long time may let you forget the original syntax too :stuck_out_tongue:

So,there are also some cons too of using templates.

You can see my template at Competitive-Programming-Setup/code_template.cpp at master · likecs/Competitive-Programming-Setup · GitHub

It works for C++11 or higher only. I have used some templates and trace functions too.


why is freopen put inside #ifdef#endif?How can it be used in the program to get input and send output?

#ifdef debug…#endif” ensures the code inside it is compiled only if “debug” is defined

why define MAX(a, b) or MIN(a, b) when we already have max(a, b) or min(a, b) in algorithm header file

1 Like
		freopen("inp.txt", "r", stdin);

Can you please explain this?

1 Like

At a guess, it could be that it makes writing the following convenient:

#include <iostream>
#include <algorithm>

using namespace std;

#define max(a, b) ((a) > (b) ? (a) : (b))

int main()
    int a = 1;
    long b = 2;

    cout << "max: " << max(a, b) << endl;

(if FIX_COMPILER_ERROR is not defined, this will be a compiler error).

1 Like

Most of online coding sites pass the compiler directive ONLINE_JUDGE while compiling your code.
This line simply changes your program to take input from ‘inp.txt’ rather than stdin (which is mostly your terminal, unless you redirected it from a file) if the directive ONLINE_JUDGE is not defined.
So when you compile locally your program will use ‘inp.txt’ as input and for online judges it will continue to use standard input, without the need for you to change your code.

1 Like