Must-Know C++ tips and tricks for Competitive Programming | Part -1

Dhruv Kothari
GDSC DYPCOE
Published in
5 min readJun 11, 2020

Note from Author(Dhruv Kothari):- Hello readers! This article is part-1 of Must Know C++ tips and tricks for Competitive Programming. All the opinions expressed in this article represent my own views which I think is appropriate to keep in mind while doing Competitive Programming.

Before starting I would recommend you to please check out my previous article on beginners' roadmap to start Competitive Programming.

1. Use <bits/stdc++.h> Header File

Let us start with the header file. <bits/stdc++.h> is a most commonly and widely used header file in competitive programming world. Basically, It is a header file that includes all the standard libraries.

All these libraries are included in <bits/stdc++.h>. We all know that time is very precious in programming contests, hence using this header file would be a clever choice to save a lot of time and avoid mistakes.

Things to keep in mind while using <bits/stdc++.h>

  • This header file contains a lot of header files which might be of no use in your code which may increase compilation time.
  • It is not the standard header file of the GNU C++ library, hence compilers other then GCC might not able to compile it. But don’t worry, this won’t happen in most cases😅😅.

2. “\n” versus endl

For most of us, both sound the same right but it’s not the case. endl takes more execution time than “\n” which can cause TLE. Here is the reason:

"\n": it inserts a new line.
"endl": it inserts a new line and flushes the stream.
So "\n" is a character whereas endl is a manipulator.
"endl" is equivalent to cout << ‘\n’ << flush;

Try problem UNITGCD: https://www.codechef.com/problems/UNITGCD. Using endl in place of “\n” will cause a TLE in the above question.

TIP:

Hence to avoid this mistake we can use define a macro:

#define endl "\n"

3. auto(Keyword)

When I started with STL, iterators were one of the toughest things to understand as well as declaring-syntax was a difficult job, and maybe the same case is with you. Here’s where auto(keyword) comes into the picture. Before C++ 11, data types were mandatory to be defined during compile-time, but later on, the auto keyword was introduced which gave us the freedom to declare a variable at runtime. So we no longer need to define the datatype. This helps us to define iterators very easily.

4. Range-based for-loops

Range-based for loops is an upgraded version of traditional for loops. They are quite similar to for loops which we use in Python. They were introduced in C++ 11. We can easily iterate over vectors, maps, and other STL containers with this for loop and auto keyword. It's really comprehensive and powerful.

Syntax:

for ( range_declaration : range_expression )

  • range_declaration: Here we have to write the data-type of the iterator which is going to iterate through the container. So we can use the “auto” keyword. It will detect the datatype during runtime.
  • range_expression: we have to mention the container on which we have to iterate.

Also, we can reverse iterate the loop by using-boost::adaptors:: reverse(v1) which is included in #include<boost/range/adaptor/reversed.hpp> Header

5. String To Integers and vice versa

Integer to String

  • The to_string() function is used to convert a number into a string. I have listed 2 more ways to convert numbers to a string.
  • For single-digit integer to string :
char c = char(num+48);          // where num is single digit integer
// like 3 or 7. So char() typecast
// {num+48}from ASCII value to char

String to Integer

  • stoi() is the function used for converting a string to an integer, for beginners it might be new, so I included that in this series.
  • For single-digit Integer:
int x = int(c)-48;      // here c is a char like ‘3’ or ‘7’.So int()
// typecast c and convert it into ASCII
// value and then we subtract 48(ASCII of
// digits start with 48) to get the integer.

6. isalnum(), isalpha(), isdigit() and many more !

These functions can be found in header <cctype>.

These functions are very useful in checking whether the character is an uppercase alphabet, lowercase alphabet, a number, and many more. These functions are really very useful while doing competitive programming. Here is the list of few must-know functions.

1. isalpha( )           // returns a non-zero value if a character
is an alphabet else return 0;
2. isalnum( ) // returns a non-zero value if a character
is an alphabet or numeric else return 0;
3. isupper( ) // returns a non-zero value if a character
is an upper-case alphabet else return 0;
4. islower( ) // returns a non-zero value if a character
is an lower-case alphabet else return 0;
5. isdigit( ) // returns a non-zero value if a character
is a numeric digit else return 0;
6. ispunct( ) // returns a non-zero value if a character
is a punctuation character else return 0;

Few more functions while use to change the case of alphabet characters.

1. toupper( )               // it is used to convert a lowercase
alphabet to uppercase alphabet.
2. tolower( ) // it is used to convert a uppercase
alphabet to lowercase alphabet.

7. C++ Code Visualizer

At the initial stage, we often found that our syntax is correct but the output is wrong, which means there is something wrong with the logic. And, sometimes it is very difficult to debug the logical error. In that case, we can use this tool through which we can visualize our code.

Link: http://www.pythontutor.com/cpp.html#mode=edit

C++ code visualizer

That’s it from this article. Hope you liked this article.

For more such tips and tricks check out my github repo https://github.com/kothariji/30-days-of-code-LinkedIn

Thank You. See you in the next one!

Part-2 will be released soon.

--

--