0% found this document useful (0 votes)
23 views3 pages

What Simple CPP Tricks Should Everyone Know

Uploaded by

Sai Pranav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views3 pages

What Simple CPP Tricks Should Everyone Know

Uploaded by

Sai Pranav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

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:

What simple C++ tricks should everyone know?

For free Interview preparation study materials check the links below:

Algorithm and Data Structures 80+ chapters: https://www.prodevelopertutorial.com/ajs-guide-to-


data-structures-and-algorithms-the-complete-guide-from-beginner-to-expert/

Coding questions 200+ solved questions: https://www.prodevelopertutorial.com/ajs-guide-to-


interview-coding-questions/

C++ Tutorial 88+ chapters: https://www.prodevelopertutorial.com/ajs-guide-to-c-programming-


for-beginners/

Linux System Programming: https://www.prodevelopertutorial.com/ajs-guide-to-linux-system-


programming/

System Design Interview questions: https://www.prodevelopertutorial.com/system-design-


interview-topics-and-examples/

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.

From C++11, there is a new way of looping using auto.


Say for example, you have a complex data structure using STL like this one:

1. map <int, vector< pair<int,int> > > myMap;


It means you have a map where the primary key is an integer and the corresponding value of
a key is a vector which again contains pair of integers.
Now, to iterate somewhere inside the code, you could use C++ iterator to declare iterator
for this map and say for example you want to point to the beginning element:
1. map<int, vector< pair<int,int> > > :: iterator it =
myMap.begin();
Instead of writing this, we can now easily write this from C++11 and onward:

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:

1. pair<int,int> myPair={1,2}; // No need of make_pair()


Interestingly, assigning values to many containers can be done by similar method in C++11.

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!

1. // Sort a set of pairs by second value by functor


2.
3. struct comp
4. {
5. bool operator()(const pii &a, const pii &b)
6. {
7. return a.second<b.second;
8. }
9. };
10.
11. set<pii,comp> s;
Now, whenever you insert some pair into this set, it will sort everything based on the second
element primarily. Of course you could swap the first and the second element and
avoid functor. In case there are cases where you need to do some complex
sorting, functor can be of great help.
You can check this out.

Click here for more Interview tips and free placement materials, join the telegram channel:

You might also like