C++ Competitive Programming : Fast C++ Template using Macros

Hello coderz, Welcome to CodingBroz where you learn new tips and tricks about Competitive Programming in C++ and many more programming related stuffs.

Today I am going to give you some awesome tips and tricks of competitive programming and tell you about C++ macros so that you can use them effectively and get an amazing edge over your competitive programming in C++ journey.

C++ Competitive Programming : Fast C++ Template using Macros

What are MACROS ?

Preprocessors are programs that process the source before compilation and macros are a type of preprocessor directives.

Macros are basically simple pieces of code in a program which is given a name and whenever the name is encountered by the compiler the compiler replaces the name with the actual piece of code it is representing.

We use “#define” directive to define a macro. Let’s understand with an example of code :

#include <bits/stdc++.h>
#define MAX 10 // Here we are defining a macro
using namespace std;

int main()
    for(int i=0 ; i < MAX ; i++){
	Cout <<  i  << “\n” ;
    return 0;

Output :


In the above program, when the compiler executes the word MAX it just replaces it with an integer value 10. The word “MAX” is called macro template and the explanation i.e “10”  is known as macro explanation.

Note : Do not use ; (semi-colons) while defining macros.

In Competitive programming, choosing C++ as a programming language gives us an edge as we can use the power of macros in C++.

It is highly recommended that you should always use macros in Competitive programming contests to save your efforts and most importantly time, which is very very important during any competitive programming contest.

Macros with parameters

Now, let’s see how you can use macros as a function or using macros with parameters. 

By using this method you can predefine many commonly used methods in a macro and use it during a competitive programming contest to save time.

Lets see with the help of a code :

#include <bits/stdc++.h>
#define area(l,b) l*b 
using namespace std;

int main()
    cout << area(10,5) ;
    return 0;

In the above program we are using macros with parameters. Whenever the compiler finds the area(l,b) in the program it replaces it by l*b

Now, whenever parameters is passed in the macro template area(l,b), it will be replaced by the statement l*b. So, when area(4,5) is encountered by the compiler it replaces and treats the line as 4*5 and then gives the result.

Using C++ macros as functions

You can also define macros to replace it with a function to save your time during competitive programming contests.

Lets see with example :

This a macro template for replacing a FOR LOOP :

#define REP(i,a,b)  for(int i=a ; i<b ; i++)

Using this above code we can replace the for(int i=a ; i<b ; i++) using REP(i,a,b)

Example – 

for(int i=1 ; i<n ; i++) cout << i << ‘\n’ ;

This above code can be easily replaced with ,

REP(i,1,n) cout << i << ‘\n’ ;

So, By this way you can use macros in competitive programming using C++.

Extra Tip#1 : The 2 lines of code :



Are used for Fast I/O or Fast Input/Output. With the help of this you can optimise your input and output time. 

To know more about fast I/O methods, you can read our article on fast input output techniques. 

Also Read:

Using Type names in C++

Another method to save your time during competitive programming is by using typedef command. Using this you can often define shorter names for data types and other parts of code.

For example : Using the data type long long int is time taking as it is too long in size, instead we can replace it with LL  to save time.

typedef  long long int  LL; 

Using the above line, instead of writing :

long long int a = 10;
long long int b = 20;

We can use,  LL a = 10; LL b = 20;  and this will save your time.

C++ Competitive Programming Templates

Using the help of macros and typedef you can become more effective in Competitive programming. C++ gives you this edge in every contest.

Here, is the template you can use or with reference to this you can design your own template :

#include <bits/stdc++.h>
using namespace std;

typedef long long LL; 
typedef pair<int, int> pii; 
typedef pair<LL, LL> pll; 
typedef pair<string, string> pss; 
typedef vector<int> vi; 
typedef vector<vi> vvi; 
typedef vector<pii> vii; 
typedef vector<LL> vl; 
typedef vector<vl> vvl; 

#define FOR(a, b, c) for (int(a) = (b); (a) < (c); ++(a)) 
#define FORN(a, b, c) for (int(a) = (b); (a) <= (c); ++(a)) 
#define FORD(a, b, c) for (int(a) = (b); (a) >= (c); --(a)) 
#define FORSQ(a, b, c) for (int(a) = (b); (a) * (a) <= (c); ++(a)) 
#define FORC(a, b, c) for (char(a) = (b); (a) <= (c); ++(a)) 
#define FOREACH(a, b) for (auto&(a) : (b)) 
#define REP(i, n) FOR(i, 0, n) 
#define REPN(i, n) FORN(i, 1, n) 
#define MAX(a, b) a = max(a, b) 
#define MIN(a, b) a = min(a, b) 
#define SQR(x) ((LL)(x) * (x)) 
#define RESET(a, b) memset(a, b, sizeof(a)) 
#define fi first 
#define se second 
#define mp make_pair 
#define pb push_back 
#define ALL(v) v.begin(), v.end() 
#define ALLA(arr, sz) arr, arr + sz 
#define SIZE(v) (int)v.size() 
#define SORT(v) sort(ALL(v)) 
#define REVERSE(v) reverse(ALL(v)) 
#define SORTA(arr, sz) sort(ALLA(arr, sz)) 
#define REVERSEA(arr, sz) reverse(ALLA(arr, sz)) 
#define PERMUTE next_permutation 
#define TC(t) while (t--) 

int main()
    int t;
    TC(cin >> t){

    return 0;

Leave a Comment

Your email address will not be published. Required fields are marked *