C++ competitive programming tips for beginners

(80 Views)


C++ is basically the superset of C language having many function libraries. The credit behind the development of this programming language goes to a Danish computer scientist Bjarne Stroustrup. It is an object-oriented programming language which makes it suitable to develop system software, servers, and so on.

C++ tips for competitive programming

C++ consists of two main components including a direct mapping of hardware features supplied by the subset of C and zero-overhead abstractions based on the mappings. Apart from that, it supports four kinds of memory management static storage duration objects, thread storage duration objects, automatic storage duration objects, and dynamic storage duration objects. C++ library contains core language and standard language. The latter one is valued more in the implementation of C++.

Even when you practice a lot, knowing a few tricks will raise your level of C++ programming. If you are wondering about how to write code productively in C++, then this article will help you in learning.

Top tips for C++ programmers

  1. Do you know that you can calculate the total number of digits in a number without using looping? Just use the log as follows-
  2. Number of digits in N =floor(log10(N)) + 1;
  3. With the given code, you can easily avoid the use of strlen():
  4. for (i=0; s[i]; i++) { }
  5. Some programmers like to use % operator for checking that a number is even or odd. It’s great but in case of large numbers, you can use the following code.
  6. if (num & 1) cout << "ODD"; else cout << "EVEN";
  7. Learn to swap two numbers by using XOR. It will help in avoiding the use of a third variable.
  8. a ^= b; b ^= a; a ^= b;
  9. If you want to compute the significant digit of any number, an easy way to use log directly.
  10. Suppose the number is N then Let double K = log10(N); now K = K - floor(K); int a = pow(10, K); a will be the most significant digit
  11. To find if a number is power of 2, we are presenting the simplest way of doing so.
  12. bool isPowerOfTwo (int x) { return x && (!(x&(x-1))); }
  13. Always remember to add a semicolon after class declarations. In C++, it works as a statement terminator that creates a need for use it many times quite often. It is necessary to end statements with a semicolon. Moreover, it is wrong to add a closing brace after semicolon but can be done if it is ending a class declaration that has a case.
  14. if (a == b) { cout << "a and b are equal."; }
  15. The iota() algorithm is quite useful in C++. It helps in generating a range of increasing values in sequence.
  16. #include <iostream> #include <numeric> int main () { int numbers[5]; std::iota (numbers,numbers+5,5); std::cout << "numbers are :"; for (int& i:numbers) std::cout << ' ' << i; std::cout << '\n'; return 0; }

    Output

    numbers are : 5 6 7 8 9
  17. Avoid the use of data promotion for controlling results. Integer operands are promoted in case of mixed-integer/floating-point expressions. Remember that this will store both as the type and therefore, promotes n and results for type double. After that, it will perform a floating-point division. This can be produced by the use of data casts-
  18. cout << results / (n / (double) 8); cout << results / (n / static_cast<double>(8));
  19. In C++, you can create assignment in binary form
  20. #include<iostream> using namespace std; int main() { auto number = 0b01; cout << number; return 0; }

    Output

    1

Practice all these given tips and increase your efficiency in programming. These are the best tips for competitive programmers and novice who have just begin to learn programming in C++.

Solution Worked 0 UpvotesUpvote

        

Solution Didn't Worked 0 DownvotesDownvote



Comments



Get Programming Help
Search

Play 2048 Game Online


Search Tags

    Competitive coding tips for c++ beginners

    Cpp tips for programmers