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 <iostream>
using namespace std;
int main() {
   long long x = 10;
   long long y = 50;
   cout << x << ", " << y;
}

Output

10, 50

Example Code (Shorted using typedef)

#include <iostream>
using namespace std;
typedef long long ll;
int main() {
   ll x = 10;
   ll y = 50;
   cout << x << ", " << y;
}

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 <iostream>
#include <vector>
#define F first
#define S second
#define PB push_back
using namespace std;
typedef long long ll;
typedef vector<int< vi;
typedef pair<int, int< pii;
int main() {
   vi v;
   pii p(50, 60);
   v.PB(10);
   v.PB(20);
   v.PB(30);
   for(int i = 0; i<v.size(); i++)
      cout << v[i] << " ";
      cout << endl;
      cout << "First : " << p.F;
      cout << "\nSecond: " << p.S;
}

Output

10 20 30
First : 50
Second: 60

Updated on: 19-Aug-2019

266 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements