Some useful C++ tricks for beginners in Competitive Programming

25 May, 2022
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 data types can save a lot of time during programming contests. When a variable is defined as auto, the compiler can be determining the datatye on its own. Example: 
When a variable is defined as auto, the compiler can be determining the datatye on its own. Example: 
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. Example:
typedef long long ll;
typedef pair w;
typedef vector va;
typedef vector vb;
typedef vector vc;
The C++ STL is a very rich library consisting of useful functions, which are super useful and time-saving during contests. For using these functions you need to include a header file. 
#include <algorithm>
We are going to discuss the most commonly used STL algorithmic functions below:-
// for binary search in containers like vector (let target element=6)
binary_search(v.begin(), v.end(), 6); 
// return 1 or 0 as present or not

// max/min of two numbers
ans = max(a,b);
ans = min(a,b);

// max/min of three numbers
ans = max(a,max(b,c));
ans = min(a, min(b,c));

// swap two numbers
swap(a,b);

// reverse containers like vectors, strings
reverse(v.begin(), v.end());

// rotate containers like vector, strings by n position
rotate(v.begin(), v.begin()+n, v.end());

// sort arrays of size n
sort(arr, arr+n);

// sort containers like vector, strings(based on intro sort)
sort(v.begin(), v.end());
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.
simranarora5sos
geeky01adarsh
CPP-Competitive-Programming
C++
Competitive Programming
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Sorting a vector in C++
Polymorphism in C++
std::string class in C++
Pair in C++ Standard Template Library (STL)
Friend class and function in C++
Competitive Programming - A Complete Guide
Practice for cracking any coding interview
Modulo 10^9+7 (1000000007)
Arrow operator -> in C/C++ with Examples
Prefix Sum Array - Implementation and Applications in Competitive Programming