What Simple CPP Tricks Should Everyone Know
What Simple CPP Tricks Should Everyone Know
For free Interview preparation study materials check the links below:
Click here for more Interview tips and free placement materials, join the telegram channel:
Checkout Algo and DS book and crack the coding interview. 88+ Chapters with Most
frequently asked HR questions answered. Just at 249 rs. Click here to know more:
1.
1. auto it = myMap.begin();
After finding out about auto, I don’t use the previous way anymore! :)
2.
To assign elements into a pair, we don’t actually need to use make_pair() function always.
This facility is probably available from C++11.
Previously:
1. pair<int,int> myPair=make_pair(1,2);
Now:
3.
C++11 has a function called to_string(). I was really happy to see this function when I
encountered it for the first time.
1. int a=12;
2. string x=to_string(a);
This function works for almost everything. Goodbye to manually converting integers,
doubles etc. to strings! :D
4.
Range based for loop is another interesting addition to C++11. It’s like Python or Java. An
example using range based for loop with auto:
Previously:
Click here for more Interview tips and free placement materials, join the telegram channel:
Checkout Algo and DS book and crack the coding interview. 88+ Chapters with Most
frequently asked HR questions answered. Just at 249 rs. Click here to know more:
1. map<int,int> myMap;
2. for (map<int,int> :: iterator it = myMap.begin(); it !=
myMap.end(); it++)
3. {
4. // say for printing
5. cout<<(it->first)<<": "<<(it->second)<<endl;
6. }
Now:
1. map<int,int> myMap;
2. for(auto &it: myMap)
3. {
4. cout<<it.first<<": "<<it.second<<endl;
5. }
Very prudent!
5.
Functor!
Click here for more Interview tips and free placement materials, join the telegram channel: