Writing code faster in C++ STL - GeeksforGeeks

20 Jul, 2017
You are wondering some time coder write his code in 2 min or less how? No kidding! Although this tip may not be very useful for competitions such as ICPC or IOI. But there are recent ICPCs ranks where rank i and rank i + 1 are just separated by few minutes. When you can solve the same number of problems as your competitor, it is now down to coding skill and ... typing speed.
Try this typing test at typingtest.com and follow the instructions there on how to improve your typing skill.
Many of them uses typedefs, shortcuts, or macros that are commonly used by competitive programmers to speed up the coding time. In this short section, we list down several examples as below.
// C++ STL shortcuts for// typing codes fast#include <bits/stdc++.h>  // Shortcuts for "common" data types in conteststypedef long long ll;typedef vector<int> vi;typedef pair<int, int> ii;typedef vector<ii> vii;typedef set<int> si;typedef map<string, int> msi;  // To simplify repetitions/loops, Note: define your// loop style and stick with it!#define REP(i, a, b) \for(int i = int(a); i <= int(b); i++) // a to b, and variable i is local!#define TRvi(c, it) \for(vi::iterator it = (c).begin(); it != (c).end(); it++)#define TRvii(c, it) \for(vii::iterator it = (c).begin(); it != (c).end(); it++)#define TRmsi(c, it) \for(msi::iterator it = (c).begin(); it != (c).end(); it++)  #define INF 2000000000 // 2 billion  // If you need to recall how to use memset:#define MEMSET_INF 127 // about 2B#define MEMSET_HALF_INF 63 // about 1B  // memset(dist, MEMSET_INF, sizeof dist); // useful to initialize shortest path distances// memset(dp_memo, -1, sizeof dp_memo); // useful to initialize DP memoization table// memset(arr, 0, sizeof arr); // useful to clear array of integers  int main(){    ios_base::sync_with_stdio(false);    cin.tie(NULL);      // YOUR CODE GOES HERE      return 0;}
Here, typedef and #define are used to short the code and you can use as per your need in main method ios_base::sync_with_stdio(false); and cin.tie(NULL); use for fast io which is reduce the time of running code.
Related Articles:
Writing C/C++ code efficiently in Competitive programming
How to begin with Competitive Programming?
Tips and Tricks for Competitive Programmers | Set 1 (For Beginners)
This article is contributed by Dhavalkumar Prajapati. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Competitive Programming
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Multistage Graph (Shortest Path)
Breadth First Traversal ( BFS ) on a 2D array
Shortest path in a directed graph by Dijkstra’s algorithm
5 Best Books for Competitive Programming
Check whether bitwise AND of a number with any subset of an array is zero or not
5 Best Languages for Competitive Programming
Most important type of Algorithms
Difference between Backtracking and Branch-N-Bound technique
String hashing using Polynomial rolling hash function
Graph implementation using STL for competitive programming | Set 2 (Weighted graph)