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("input.txt","r",stdin);freopen("output.txt","w",stdout);
    freopen("log.txt", "w", stderr);
    #endif
    int t,i,j;preprocess();
    scanf("%d",&t);
    while(t--)
    {
       compute();
    }//end while
    #ifdef debug
    fprintf(stdout,"\nTIME: %.3lf sec\n", (double)clock()/(CLOCKS_PER_SEC));
    #endif
    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))

~
REGARDS
Prakhar Avasthi

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:

2 Likes

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

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

YFUvvfgxvhcuygt

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…

3 Likes

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

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:

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).
and

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.
[1]:http://codeforces.com
[2]:https://code.google.com/codejam/contests.html
[3]:http://en.cppreference.com/w/

You can see my template at https://github.com/likecs/Competitive-Programming-Setup/blob/master/code_template.cpp

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