Table of contents
1.
Introduction
2.
What is Boilerplate code?
3.
Parts of Boilerplate code
3.1.
Header Files
3.2.
typdef
3.3.
templates
3.4.
#define
3.5.
Fast I/O 
4.
FAQs
5.
Key Takeaways
Last Updated: Mar 27, 2024

Boilerplate Competitive programming

Author Saksham Gupta
2 upvotes
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

If you are into sports, then competitive programming is definitely for you; the only difference here is that it is more of a mind sport where you have to solve complex problems using complex algorithms. But before getting started, you should know about the boilerplate code, which helps to reduce time during problem-solving.

Recommended topic, kth largest element in an array and Euclid GCD Algorithm

What is Boilerplate code?

The term "boilerplate code" refers to code that can be reused multiple times. Anyone, on the other hand, could claim that it is a reusable piece of code. Basically, during competitive coding, you have to write a lot of code, again and again, this costs a lot of time and you can't afford to waste time during a contest. Thus, we use a boilerplate code beforehand and use this instead of re-writing similar base code. Now we have learned the importance of boilerplate code, let's also learn how to write one by seeing different parts of it.

Parts of Boilerplate code

Header Files

A header file, with the extension. h, includes C++ function declarations and macro definitions that can be shared across several source files. The preprocessing directive #include is used to include both the user and system header files. For example

#include <string>
#include <vector>
You can also try this code with Online C++ Compiler
Run Code


Now <string> header will contain all the functions related to string, whereas <vector> header file will contain all the functions related to vectors. There are a lot of other header files, and typing them every time costs us a lot of time. Thus we use <bits/stdc++.h> file, which contains all the libraries in one place.

typdef

First, we define typedefs so that we don't have to write long words for these. 

But what is a typedef?
Now a typedef is which you can use to give a type a new name. For example, 
typedef long long ll;
Now instead of long long I can simply write ll

Below are various types of typedef that we use for our boilerplate code.

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;
You can also try this code with Online C++ Compiler
Run Code

templates

In C++, a template is a basic but powerful tool. The basic idea is to pass the data type as a parameter, eliminating the need to write the same code for multiple data types. A software business, for example, may require sort() for many data kinds. We can construct a single sort() function and pass data type as a parameter instead of writing and maintaining numerous codes. Below are the examples of templates that we use in boilerplate code.

template<typename X, typename Y> inline void amin(X &a, Y b) { if(b< a) a = b; }
template<typename X, typename Y> inline void amin(X &a, Y b) { if(b> a) a = b; }
You can also try this code with Online C++ Compiler
Run Code

#define

The #define command is used to make substitutions in the file where it is found. In other words, #define instructs the compiler to go through the file and replace macro-name with replacement-string wherever it appears. At the end of the line, the replacement string comes to an end.

For example
#define FOR(i, j, k, in)  for(int i=j ; i<k ; i+=in)
Now instead of writing for(int i=j ; i<k ; i+=in) we can simply write FOR(i, j, k, in).

Below are the examples of all the #define that we use in boilerplate code.

#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>()
You can also try this code with Online C++ Compiler
Run Code

Fast I/O 

In competitive programming, it's critical to read input as quickly as possible in order to save time. "Warning: Large I/O data, be careful with specific languages (but most should be OK if the technique is adequately written)," you've probably read in numerous problem descriptions. Faster I/O techniques are the key to solving such issues. Below is the code we use for fast input outputs.

template <typename X> inline void write(X a)
{
    int j = 21;
    char buffer[22];
    buffer[21] = '\0';
 
    do
    {
        buffer[--j] = a % 10 + '0';
        a/= 10;
    }while(a);
    do
    {
        putchar(buffer[j]);
    } while (buffer[j++] != '\0');
}
 
template <typename X> inline X readInt()
{
    X nt=0,a=1;
    char to_take=getchar();
    if(to_take=='-')
        a=-1;
    while((to_take<'0'||to_take>'9')&&to_take!=EOF&&to_take!='-')
        to_take=getchar();
    if(to_take=='-')
        a=-1,to_take=getchar();
    while(to_take>='0'&&to_take<='9') {
        nt = (nt<< 3) + (nt<< 1) + (to_take - '0');
        to_take=getchar();
    }
 
    return nt*a;
}
You can also try this code with Online C++ Compiler
Run Code


Also instead of adding this to our code, we can also use a smaller version of this which is given below.

ios_base::sync_with_stdio(false);
cin.tie(NULL);
You can also try this code with Online C++ Compiler
Run Code


But how are we gonna read input using this?
We can take the input as shown in the below snippet.

int tc;
    tc = read(int);

    while(tc--){
        write(tc);
    }
You can also try this code with Online C++ Compiler
Run Code

Check out this problem - Minimum Coin Change Problem 

FAQs

  1. What is Boilerplate code?
    The term "boilerplate code" refers to code that can be reused multiple times. Anyone, on the other hand, could claim that it is a reusable piece of code. Basically, during competitive coding, you have to write a lot of code, again and again, this costs a lot of time, and you can't afford to waste time during a contest.
     
  2. What is competitive programming?
    Competitive programming is a mind sport in which people compete to program according to given requirements over the Internet or a local network. Sport programmers are the names given to the contestants.
     
  3. What are different topics used in competitive programming?
    Basics, Greedy and Bit Manipulation, Number Theory and Combinatorics, Searching, Sorting, and Basic Data Structures, Tree and Graphs, Recursion and Dynamic Programming, String Algorithms, Geometry, and Game Theory, Advance Data Structures are topics used in competitive programming. 
     
  4. Can we code without using the boilerplate code?
    Of course, you can code without any problem even without a boilerplate code but it will cost you a lot of time and time is money in competitive programming. Thus the choice is yours.
     
  5. Is there any other Data Structures and Algorithms content in Coding Ninjas Studio?
    Yes, Coding Ninjas Studio allows you to practice coding as well as answer frequently asked interview questions. The more we practice, the more likely we are to acquire a job at our dream company.

Key Takeaways

We saw how we could solve competitive programming problems faster using boilerplate code, we saw different parts of a typical boilerplate code in detail. Now it's your time to prepare your own boilerplate code and rock this competitive world, So, don't waste any more time and head over to our practice platform Coding Ninjas Studio to practice top problems on every topic, interview experiences, and many more. You can also consider our competitive programming course to give your career an edge over others!
Till then, Happy Coding!

Live masterclass