Here are some of the basic C++ tricks that every beginner in Competitive Programming should follow for increased speed. However, competitive programming can only be mastered with time and lots of practice. These tricks will just help you to save a little time during the contests which sometimes matters a lot.
- Using the auto keyword to declare datatypes can save a lot of time during programming contests.
When a variable is defined as auto, compiler can determing the datatype on its own.
auto a = 100; // a will become 'int' auto b = 1LL; // b will become 'long long' auto c = 1.0; // c will become 'double' auto d = "variable"; // d will become 'string'
- The watch macro is one of the most useful tricks ever.
#define watch(x) cout << (#x) << " is " << (x) << endl
If you’re debugging your code, watch(variable); will print the name of the variable and its value. (It’s possible because it’s build in preprocessing time.)
Using typedef’s can save a lot of time of yours which you might spend re-writing the same snippet again and again.
typedef long long ll; typedef pair w; typedef vector va; typedef vector vb; typedef vector vc;
- There’s an inbuilt function to evaluate the greatest common divisor of two numbers. It’s called __gcd() and it’s present in the algorithm header file. To read more about it, refer: https://www.geeksforgeeks.org/stdgcd-c-inbuilt-function-finding-gcd/
- Using “\n” for adding new line breaks is much faster than using “endl”.
- If you use ios::sync_with_stdio(false) at the beginning of your code, you’ll make cin and cout as fast as printf and scanf, but you’ll no longer be able to use neither printf nor scanf.
- Tips and Tricks for Competitive Programmers | Set 1 (For Beginners)
- Tips and Tricks for Competitive Programmers | Set 2 (Language to be used for Competitive Programming)
- C++ tricks for competitive programming (for C++ 11)
- Bit Tricks for Competitive Programming
- Common mistakes to be avoided in Competitive Programming in C++ | Beginners
- Java tricks for competitive programming (for Java 8)
- Python Tricks for Competitive Coding
- How can competitive programming help you get a job?
- How to begin with Competitive Programming?
- How to become a master in competitive programming?
- Fast I/O for Competitive Programming
- Python in Competitive Programming
- A Better Way To Approach Competitive Programming
- Fast I/O in Java in Competitive Programming
- Bitwise Hacks for Competitive Programming
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.