Open In App

Writing code faster during Competitive Programming in C++

Improve
Improve
Like Article
Like
Save
Share
Report

This article focuses on how to implement your solutions and implement them fast while doing competitive programming.

Setup Please refer Setting up a C++ Competitive Programming Environment

Snippets Snippet is a programming term for a small region of re-usable source Code. A lot of modern text editors like Sublime provide you a functionality to automatically write a predefined snippet by just writing a keyword. One can speed up their implementation by using Snippets. So if you need to do a BFS or DFS(or any other code), you just need to press ‘Keyword+Tab’ to get it’s code.

How to add a snippet Adding a snippet in Sublime Text is pretty straightforward. Just navigate to Tools>Developers>New Snippet.

<snippet>
<content><![CDATA[

// Type your code here

]]></content>
<tabTrigger>ENTER KEYWORD HERE</tabTrigger>
</snippet>

Use this as your base template for a snippet and paste the code that you want to use in your Snippet by replacing the comment and trigger Keyword.

Macros
They are a way to assign shorter notations to things that are usually longer to write.
They can be easily written using #define.

Example 1:
Before using Macros:




long long int a;
vector<long long int> v;
map<long long int, long long int> mp;


After using Macros:




// ll can be used instead of typing long long int
#define ll long long int
  
ll a;
vector<ll> v;
map<ll, ll> mp;


Example 2:
Before using Macros:




for (int i = 0; i < N; i++)
    cin >> a[i];
  
for (int i = 0; i < N; i++) {
    for (int j = 0; j < M; j++) {
        cout << m[i][j] << endl;
    }
}


After using Macros:




// Use this macro instead of typing the whole
// for-loop syntax
#define FOR(a, c) for (int(a) = 0; (a) < (c); (a)++)
  
FOR(i, N)
cin >> a[i];
  
FOR(i, N)
{
    FOR(j, M)
    {
        cout << m[i][j] << endl;
    }
}


Example 3:




vector v;
  
for (int i = 0; i < N; i++) {
    cin >> x >> y;
    v.push_back(make_pair(x, y));
}


After:




#define ll long long int
#define MP make_pair
#define pb push_back
  
vector<ll, ll> v;
  
for (int i = 0; i < N; i++) {
    cin >> x >> y;
    v.pb(MP(x, y));
}


Note: Write your macros at the beginning of your code(refer to the template given below).

Using a Template
Using a template is one of the best idea to speed up your implementation. Keep your template ready that has your macros written in them already. This helps in reducing a lot of your time which otherwise would have gone in writing the whole code from scratch.
I use the following template, you can use one that suits you.




#include "bits/stdc++.h"
using namespace std;
#define max(a, b) (a < b ? b : a)
#define min(a, b) ((a > b) ? b : a)
#define mod 1e9 + 7
#define FOR(a, c) for (int(a) = 0; (a) < (c); (a)++)
#define FORL(a, b, c) for (int(a) = (b); (a) <= (c); (a)++)
#define FORR(a, b, c) for (int(a) = (b); (a) >= (c); (a)--)
#define INF 1000000000000000003
typedef long long int ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
#define F first
#define S second
#define PB push_back
#define POB pop_back
#define MP make_pair
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int T;
    cin >> T;
    while (T--) {
        int N;
        cin >> N;
        ll a[N];
        FOR(i, N)
        cin >> a[i];
    }
    return 0;
}


Thank you for reading, please let me know if you have some possible improvement or addition.



Last Updated : 11 May, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads