what is the best way to find shortcuts in c++? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

what is the best way to find shortcuts in c++?

best ways to find c++ code tricks or shortcuts which can make program shorter.

3rd Apr 2018, 8:19 PM
Ashutosh kumar
Ashutosh kumar - avatar
5 Answers
0
1.STL : If shorter means reducing the length of the code you have to write, c++ offers STL for commonly used algorithms like search, sort, finding maximum minimum, set operations etc. For instance : Instead of writing youe own sorting function. To sort an array of size 'n' simply we can use sort(array, array +n) ; STL also offers common data structures so you don't have to implement them from scratch For instance : if you want a stack of int, you can simply include stack header and make stack instead of writing your own stack as : stack <int > st; The advantages of STL is it is efficient and less error prone as it is regularly maintained by active cpp community. 2.Loops : I find range based loop more elegant and appropriate whenever I can use them . Suppose we have to output an integer array of size n; for(int i = 0; i < n; i++) cout << array[i] ; This can be written in much elegant way : for(int element :array) cout << element; 3.typedef : Suppose I have to declare a unsigned long long variable I can do sth like this. unsigned long long a, b; Instead of writing unsigned long long in every place in my code. I can use typedef unsigned long long ull; and declare variables as: ull a, b;
4th Apr 2018, 10:31 AM
Bishal Sarang
Bishal Sarang - avatar
+ 1
StackOverflow.com Is my webpage of choice to satisfy my code needs.
3rd Apr 2018, 9:20 PM
Bebida Roja
Bebida Roja - avatar
+ 1
Note that shorter does not always mean better.
4th Apr 2018, 10:26 AM
Bebida Roja
Bebida Roja - avatar
0
What do you mean by finding shortcuts in c++?
3rd Apr 2018, 8:22 PM
Bishal Sarang
Bishal Sarang - avatar
0
i mean are there any ways to effectively make your code a bit shorter.
4th Apr 2018, 10:09 AM
Ashutosh kumar
Ashutosh kumar - avatar