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.
5 Antworten
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;
+ 1
StackOverflow.com Is my webpage of choice to satisfy my code needs.
+ 1
Note that shorter does not always mean better.
0
What do you mean by finding shortcuts in c++?
0
i mean are there any ways to effectively make your code a bit shorter.