8 Amazing C++ Tricks to use in a programming contest

Programming contests can manage their time for each programmer. It is very rare that a programmer starts programming when he sees the puzzle or the problem. Because the main specialty of becoming a programmer is to learn the design process. What this means when you solve the puzzle in your head, you are ready to solve these puzzles with your code.

So here is the problem, contests can be divided into two main axes:
1) Design process
2) Encoding process

Since the coding of an already designed problem can be very simple, everything becomes a time management.

In this publication, you will find tips to help you make the most of your time at competitions. In addition, technical interviews can be linked with these tricks, so the interviewer can see what you really know. It will be fun, so let’s start


1) Using auto to declare dataypes can save a lot of time during the programming competitions.

If a variable is set to auto, the compiler determines its type at the time of compilation. For example,

2) This is extremely useful with regard to iterators.

To card on card> card instead of writing this,

it is possible to write this.

There is an even shorter way to write it.

This type of loops is called loops based on range, previously compatible with Java. Note that this is not an iterator in loops based on range. So, to access the items, you should be used instead.

3) Loop in C-string

4) Testing if not negative 1

This works because the negative numbers use the complement two, so -1 is represented as all in the binary. The tilde (~) operation reverses all bits of the variable so that -1 becomes zero and any other number becomes nonzero. The if condition is valid if it is not null, so x holds for any value other than zero.

5) Order array without losing original order

This is extremely useful!

6) Making Input Output faster

If you use cin and cout for I/O, just add the line:

just after the main()
and see the magic.
I/O become nearly as fast as using scanf() and printf().

7) Simplification with predefined

Most algorithms in the STL are very useful (eg sorting, searching, copying, account, transforming, …), but to be as general as possible, they instead take a number of iterators a container, leaving most of the time it ends writing things like:

as you usually want to sort the whole container. Having a single macro like:

simplifies the command to:

which is also easier to read.

8) Get rid of those includes!

Simply use

This library includes many of libraries we do need in contest like algorithm, iostream, vector and many more. Believe me you don’t need to include anything else!

Leave a Reply

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