Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c++
communitycreator

Pointers for competitive programming C++

Harsh Jain

This shot will discuss certain tips and tricksApplied to C++ to help you become a good competitive programmer who performs well in various online coding contests and coding interview tests.

Tip 1: Use \n instead of endl

When you submit the coding solution on an online coding platform, we get to see the time it took to pass all the test casesThe time taken by the code to execute completely. In coding interview tests, this time is critical and may be a deciding factor. So, you should attempt to reduce the execution time as much as possible.

One solution is to avoid the use of endl to print the next line character in your solution. This is because the endl operator prints the new line character, but it also flushes the output stream, which leads to a higher execution time. For a lower execution time, use \n.

Tip 2: Use bits/stdc++.h

When working with libraries in C++like vector, pair, map, etc., you need to import each of them individually. However, you can import just one librarySee example below through the use of bits/stdc++.h, the header file for all the libraries in C++.

Tip 3: Use typedef

Generally, in competitive coding contests, you do not have time to declare long data type declarations like vector<int, int> or vector<pair<int, int>, bool>.

Instead, use typedef to declare these type names at the start of your program. Then, they can easily be used whenever you require them in the rest of the code.

Let’s look at the code below to better understand this.

Code

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<pair<int, bool>> vpib;

int main() {
  ll a = 123456789;
  cout << a << "\n";
  vi vec = {1,2,3,4,5};
  vpib vpair = {{1,true}, {2, false}};
  return 0;
}
Use typedef to shorten the code

Explanation

  • In line 1, we used bits/stdc++.h to import all the header files instead of importing each of them one by one.

  • From lines 4 to 6, we used typedef to declare the type names.

  • In the main() function, we used the type namesDeclared in the previous step to create variables of those types.

Tip 4: Use macros

Macros replace certain statements in the code before the compilation. They help reduce the effort of writing the same thing again and again in the code.

For example, imagine you have a vector and you use the push_back() method to insert an element into the vector. Here, you can create a PB macro that will be replaced every time as push_back. Macros can also have parameters.

Let’s look at the code below to understand this better.

Code

#include <bits/stdc++.h>
using namespace std;

#define PB push_back
#define MP make_pair
#define FIN(a , b) for(int i = a; i < b; i++)

int main() {
  vector<int> vec;
  vec.PB(10);
  vec.PB(20);

  FIN(0, vec.size())
  cout << vec[i] << " ";
  return 0;
}
Use macros in C++

Explanation

  • In lines 4 to 6, we created three macros. In line 6, we created a macro with parameters.
  • In lines 10 and 11, we used the PB macro instead of using push_back.
  • In line 13, we used a macro to run a loop.

Macros can thus be useful to reduce repeated code and write the code faster.

Tip 5: Use cin in a loop

In many problems, we are given a stream of inputs where we do not know how many inputs there are. We can use cin in a loop, where the loop accepts the input until your input becomes NULL. Let’s look at the code below to understand this better.

Code

Enter a number below.

#include <iostream>
using namespace std;

int main() {
  // your code goes here
  int n;
  while(cin >> n){
    cout << n << " ";
  }
}

Enter the input below

Use cin in a loop

Explanation:

  • In line 7, we set the condition of the while loop to run until there are no more inputs present.

This will allow you to enter any number of inputs.

These tips will hopefully help you to write code in better and faster ways.

Check out my course Competitive Programming - Crack Your Coding Interview, C++ to help you prepare for your upcoming coding interviews.

RELATED TAGS

c++
communitycreator
RELATED COURSES

View all Courses

Keep Exploring