Writing code faster during Competitive Programming in C++

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


After using Macros:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Example 2:
Before using Macros:

filter_none

edit
close

play_arrow

link
brightness_4
code

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;
    }
}

chevron_right


After using Macros:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
    }
}

chevron_right


Example 3:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


After:

filter_none

edit
close

play_arrow

link
brightness_4
code

#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));
}

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

#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;
}

chevron_right


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

competitive-programming-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


11


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.