C++ Tips & Tricks for Beginners
9 December, 2022
1
1
0
Contributors
Hello Coders !!!
If you have just started your journey of learning C++ programming language then here are some useful tricks to enhance your coding style.
1. Single header file
If you often get bamboozled in recalling the particular header files then in spite of using individual header files you can use a single header file.
This will include all the header files.
2. Omission of curly braces
In C++ while we are dealing with loops or conditional statements and we are having only a single statement to be executed, then we can skip the use of curly braces.
Here, is an example:
•
For conditional statements
•
In loops
3. Conditional or Ternary Operator
While writing an if-else statement in our code we can use a one-liner conditional or ternary to do the same.
Here is the syntax of the conditional operator :
Expression1?expression2:expression3;
Here, it signifies that if expression 1 is true then expression 2 will be executed else expression 3 will be executed.
Let's see a practical example :
4. Initialising all the elements of the array with the 0
We often use loops to initialise all the elements of the array with the same value.
In spite of doing so, we can do it in line of code too.
Here is an example of the same :
Note :
In method 4 we have used memset function which can be used to initialise the array with 0 or -1 only.
Check out more about the memset function here :
In the case of vector data structure, we can initialise it with any value using this syntax :
vector<int> v (size, value)
Here is an example regarding the same.
Suppose we want to create a vector of size 5 and initialise all values with 2 then we can do this in the following way...
5. Copying array to a vector
Apart from using a loop, we can copy the whole array to a vector using this code :
If we wished to copy one vector to another then we can use the assignment operator i.e. "=" to do so.
6. Maximum element of an array of vector
In spite of writing code for traversing the whole array or vector and finding the greatest element of it, we can do this in one line of code 😉.
We can find the greatest element of an array or vector in a single line of code using the max_element function of c++.
Syntax:
template <class ForwardIterator> ForwardIterator max_element (ForwardIterator first,ForwardIterator last);
7. Greatest Common Divisor (GCD)
We can find the GCD of two numbers using the inbuilt function of C++.
Syntax for C++14 :
__gcd(number1,number2)
It is available in the 'algorithm' library.
Syntax for C++17 :
gcd(number1,number2)
It is available in the 'numeric' library.
8. String to integer and vice versa
C++ has got inbuilt functions to convert string data type to integer data type and vice versa.
•
string to int data type conversion is done using the stoi() function.
•
int to string data type conversion is done using the to_string() function.
Let's see an example of this :
Conclusion
Hope you find this blog useful for you and have learned something new. I will keep adding more to the list whenever I will find something useful like this.
Do comment with your thoughts and suggestions.
Let's connect 🚀
develevate
toolstipstricks