- Related Questions & Answers
- C++ tricks for competitive programming
- Some useful C++ tricks for beginners in Competitive Programming
- Python Tricks for Competitive Coding
- Python Input Methods for Competitive Programming?
- Input/Output from external file in C/C++, Java and Python for Competitive Programming
- Writing C/C++ code efficiently in Competitive programming
- BFS using STL for competitive coding in C++?
- C++11 reverse range-based for-loop
- C++: Methods of code shortening in competitive programming?
- C Program for Egg Dropping Puzzle - DP-11?
- Essential Python Tips And Tricks For Programmers?
- Essential Java Tips and Tricks for Programmers
- C++11 Overview
- Why is python best suited for Competitive Coding
- Tips for Students to Ace any Competitive Examination

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Here we will see some good tricks of C++ programming language that can help us in different area. Like if we want to participate in some competitive programming events, then these tricks will help us to reduce the time for writing codes. Let us see some of these examples one by one.

Checking whether a number is odd or even without using % operator. This trick is simple. We can perform bitwise AND operation with the number and 1. If the result is non-zero then this is odd, otherwise this is even. The logic is too simple. All odd numbers have 1 at the LSb. So after performing AND with 1, it will mask all characters except LSb so we can get the desired results easily.

if ((n & 1) != 0) { //this is odd } else { //This is even }

Fast multiply and divide using shift operator. If we want to multiply a number with a number like 2n then we can simple shift the number to the left n times. Similarly, if we want to divide a number by 2n then shift the number to right n times.

x = 40; y = x << 2; //x will be multiplied with 4, so y = 160 cout << x; x = 40; y = x >> 2; //x will be divided by 4, so y = 10 cout << x;

We can swap two numbers without using third variables. This can be done using + and – operators. But we can do it using bitwise XOR operators also. You can check the numbers manually to make sure.

//swap x and y x ^= y; y ^= x; x ^= y;

Sometimes there are some constrains that, we cannot use the strlen() function in our code. In that case we create our own strlen() function. If the case is only accessing the characters, then we really don’t need to do this. We can check whether the character at position i is valid (non-zero) or not. If this is non-zero we can traverse, otherwise stop.

for(int i = 0; s[i]; i++){ cout << s[i]; }

Most often we use push_back() function in STL to add new element in some containers like vector etc. Without using that, we can use emplace_back() also. This function is much faster. This does not allocate memory somewhere else, it appends allocated memory in the container.

C++ provides inbuilt GCD function. We can use them in different cases. The syntax is like below.

__gcd(x, y) //find GCD of x and y

The maximum size of an array in main function is of the order of 10^6. But if the array is declared globally, we can declare the size up to 10^7.

We can calculate most significant digit of any number using log operation. See the following logic to get the idea

n = 4578; double k = log10(n); k = k – floor(k); int x = pow(10, k); //x is the most significant digit

Directly calculate number of digits using log operation. We do not use any loop for this.

n = 4578; int digit_count = floot(log10(n)) + 1

We can check the number is power of 2 or not directly using this logic.

x = 1024; bool check = x && (!(x & (x-1))); //if this is true, then power of two.

Some inbuilt algorithms are there in C++, that can check the following conditions.

all_of(left, left + n, isPositive()); //check all are positive or not any_of(left, left + n, isPositive()); //check at least one positive or not. none_of(left, left + n, isPositive()); //check no elements are positive

Copy function to copy elements from one container to another.

int src[5] = {10, 20, 30, 40, 50}; int des[5]; copy_n(src, 5, dest);

There is an algorithm called itoa(). This algorithm can be used to create a range of sequentially increasing values as if by assigning initial value to *first, then using the value by using post increment operator.

int arr[5] = {0}; char str[5] = {0}; itoa(arr, arr+5, 15); //it will generate {15, 16, 17, 18, 19} itoa(str, str+5, ‘A’); //it will generate {‘A’, ‘B’, ‘C’, ‘D’, ‘E’}

Assign value in Binary form. We can use 0b prefix with some binary number to denote that the number is provided in binary.

int x = 0b1101; //then x will hold 13

In C++, we can use keywords without using conditional operators. Like keyword ‘and’ can be used in the place of ‘&’.

x = 10; if(x < 10 and x > 5) cout << “True” << endl; else cout << “False” << endl; //This will return True

Advertisements