C++: Methods of code shortening in competitive programming?

In this section we will see some examples of code shortening strategy for competitive programming. Suppose we have to write some large amount of codes. In that code, we can follow some strategy to make them more short.

We can change the type-name to make it short. Please check the code to get the idea

Example Code

#include 
using namespace std;
int main() {
   long long x = 10;
   long long y = 50;
   cout 

Output

10, 50

Example Code (Shorted using typedef)

#include 
using namespace std;
typedef long long ll;
int main() {
   ll x = 10;
   ll y = 50;
   cout 

Output

10, 50

So after that, we can use ‘ll’ without writing the ‘long long’ again and again.

Another example of using typedef is like below. When we write template or STL function, we can also use the macro for code shortening. we can use them like below.

Example

#include 
#include 
#define F first
#define S second
#define PB push_back
using namespace std;
typedef long long ll;
typedef vector

Output

10 20 30
First : 50
Second: 60
Updated on: 2019-08-19T14:35:42+05:30

496 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements