Writing code faster in C++ STL
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 contests typedef 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.
Recommended Posts:
- getchar_unlocked() - faster input in C/C++ for Competitive Programming
- Writing C/C++ code efficiently in Competitive programming
- C++: Methods of code shortening in competitive programming
- Check if the rows of a binary matrix can be made unique by removing a single column
- Find out the prime numbers in the form of A+nB or B+nA
- Count of triples (A, B, C) where A*C is greater than B*B
- Number of Subarrays with positive product
- Write a function to get the intersection point of two Linked Lists | Set 2
- Make the list non-decreasing by changing only one digit of the elements
- Count occurrences of a prime number in the prime factorization of every element from the given range
- Find two co-prime integers such that the first divides A and the second divides B
- Sum of all natural numbers from L to R ( for large values of L and R )
- Interactive Problems in Competitive Programming | Set 2
- Check if a cycle of length 3 exists or not in a graph that satisfy a given condition



