Great tips for coding in C++


#1

I have been solving problems and challenges on CodingGame in C++ for a while now and I thought I would like to give back something to the CG community.It is slightly a long post but I guarantee that it will help improve, so do take some time to read this.

So here are a small list tricks that you can use to optimize your coding efficiency. I have tried to sort them in increasing order of difficulty


Macros :

Loop Macros recycle

Macros are some of the most simplest way to simplify your code and also to type faster. I usually use them when there are long lines of code which keep repeating.So here are a few simple macros that you can use :

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

This macro is probably super useful because it makes creating a for loop look like child's play.This simply iterates starting from value a to b (both inclusive)

Another similar macro is :

#define REP(i,n) FOR(i,0,(int)n-1)

This should be declared after the first one.Although this looks similar to the first one I its really helpful to have this.It simply loops from 0 to n-1 This makes reading arrays very easy.

int a[10000];
REP(i,n)
    scanf("%d", &a[i]);

See its that simple. These 2 MACROS are very useful and I would highly recommend that you use them to write simpler and faster code.

If you want to loop in reverse then you can use this macro :

#define FORD(i,a,b) for(int i=a;i>=b;--i)

bug Debuging Macro beetle

Everyone knows debuging the error is a pain in the ass already and to sit and type fprintf(stderr, ...); everytime is a real bum.So you could use a neat macro like :

#define DB(s,x) fprintf(stderr,s,x)`

Most of you already know this and probably use a similar macro in your code. But the problem comes when you have more than 1 argument for your debug message. For example if you want to print out the x and y coordinates variable you will have to do something like.

DB("X = %d\n", x);
DB("Y = %d\n", y);

Most of them don't mind doing this. But what if there is a magical way to pass more than 1 argument to your debug macro ? That would save a lot of time and make your code neat and clean. And the answer is YES, there is a way to do that and it is :

#define DB(format,...) fprintf(stderr,format, ##__VA_ARGS__);

The __VA_ARGS__ stands for variable arguments and you can pass any number of arguments just like you do to a printf() function. So you could now use that macro like :

DB("Inside function");
DB("x = %d", x);
DB("a = %d | b = %d | c = %d | d = %d |", a, b, c, d);

List of a few super useful macros

  1. #define SORT(a,n) sort(begin(a),begin(a)+n) Simple way to sort arrays/vector,etc
  2. #define REV(a,n) reverse(begin(a),begin(a)+n) Simple way to reverse an arrays/vector,etc
  3. #define MS0(x) memset(x,0,sizeof(x)) To initialize the value of an entire array to 0
  4. #define ll long long To avoid typing long long every god damn time
  5. #define pb(x) push_back(x) To add an element to a vector (See STL section)
  6. #define mp(a,b) make_pair(a,b) To make a pair (See STL section)

Power of S.T.L muscle

I know to a beginner this sound like something straight out a Sci-Fi movie.Most of you might have heard this word somewhere or the other. It stands for Standard Template Library The

At first I was intimidated by STL. I was like what is this cool thing that everyone is talking about. Basically it creates a container for a specific data structure and has all the functions that the data structure needs, like push(), pop(), insert(), erase(), etc

They are super useful and is must if you want to solve Hard and Very-Hard problems. You have no idea how many times i used the queue stl to implement a quick BFS I did some googling and found some nice tutorials on YouTube :

There are many more STL containers available but I think these are sufficient for most problems.


white_check_mark General Tips

  • Try to understand how to use reference websites. I personally use http://www.cplusplus.com/reference/ to look up something that I am not sure or I just google it.
  • Always use built-in functions rather than writing your own implementation. Not only does this save time but its also much faster and is highly,highly optimized.

My Template scroll

Usually every time I start a new problem I have like a template that I use so that I can get started faster and not have to worry about typing #include< blah blah blah> every time I begin.Here is my template, feel free to modify it as you see fit smile

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
#include <time.h>
#include <assert.h>
#include <algorithm>
#include <iostream>
#include <queue>
#include <map>
#include <stack>
#include <vector>

using namespace std;

#define FOR(i,a,b) for(int i=a;i<=b;++i)
#define FORD(i,a,b) for(int i=a;i>=b;--i)
#define REP(i,n) FOR(i,0,(int)n-1)
#define #define DB(format,...) fprintf(stderr,format, ##__VA_ARGS__);
#define MS0(x) memset(x,0,sizeof(x))
#define SORT(a,n) sort(begin(a),begin(a)+n)
#define REV(a,n) reverse(begin(a),begin(a)+n)
#define ll long long
#define sp system("pause")

struct point{
    int x, y;
};

int a[100000];

int main(){

    int n;
    scanf("%d", &n);

    while(1){
        //Game Loop	
    }
    return 0;

}

Thats it for now, hopefully I helped someone atleast. I shall try my best to keep this updated. Feel free to share your own tips and tricks or ask doubts on this thread. grin +1


#2

That's awesome, thanks a lot, great article, brofist facepunch


#3

Great post thanks!

I was lead here looking for "allowed" libraries.

Do all those listed in your "template" work with codinggame.com IDE/compiler? Are those "included" with those indicated on the FAQs page (‑lm, ‑lpthread, ‑ldl, ‑lcrypt)?


#4

UPDATE : STL is the worst possible thing on CG

It turns out that CG does not use the -O3 compiler flag, which is used to optimize all STL containers and std functions like min, max, sort, etc.

More info here : https://www.codingame.com/forum/t/c-and-the-o3-compilation-flag/1670/34