

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
- Related Questions & Answers
- Writing C/C++ code efficiently in Competitive programming
- Python Input Methods for Competitive Programming?
- C++ tricks for competitive programming
- C++ tricks for competitive programming (for C++ 11)?
- Some useful C++ tricks for beginners in Competitive Programming
- Input/Output from external file in C/C++, Java and Python for Competitive Programming
- Methods in Dart Programming
- Code indentation in Lua Programming
- String Methods in Dart Programming
- Math class methods in Java Programming
- Private and final methods in Java Programming
- BFS using STL for competitive coding in C++?
- How do we format programming code using the <code> tag in HTML?
- C++ Program to check joke programming code is generating output or not
- Python Tricks for Competitive Coding
Advertisements