You must have heard of Dictionaries in Python. But have you ever wondered about dictionaries in C++?
Well, in this tech blog we are going to tell you all about C++ Dictionaries from scratch. Be it the syntax, their working, or step-by-step creation of dictionaries, this tech blog has covered pretty much everything!
So, grab a hot cup of coffee and keep your C++ Compilers ready, 'cause it's going to be a wild ride! We'll go through all the topics and by the end of this article, you'll be acquainted with C++ Dictionaries like nothing else.
What is a C++ Dictionary?
Maps in C++ actually work just like a Dictionary when the datatype of all elements in it is the same. It acts like a container that stores values which are indexed with the help of keys. Each value in a particular container is associated with a unique key.
It should be taken care of that all keys in a C++ map should be of the same data type. However, it is not necessary that both keys and values are of the same type. To use maps in C++, a header file specified for maps should be included in the standard library.
Syntax of Dictionary in C++
Here is the syntax for using Dictionary:
map<datatype_of_key, datatype_of_value>name_of_map;
Working on Dictionary in C++
Dictionaries in C++ work in accordance with the following points:
- The Dictionary type present in C++ language is called a Map that acts like a container that stores values indexed by unique keys.
- Each value in the dictionary is associated with a unique key.
- The data type of all values should be the same and of keys should be the same.
- To use this feature of C++, a specified header file should be included in the C++ Standard Library.
- Using a loop, the values in the map can be iterated where each iterated item represents a pair of keys and values.
Creating a Dictionary in C++
We have listed the following steps for creating a C++ Dictionary. Follow them to create one of your own:
01) Including Header File
Before beginning or initializing any variable, the first and foremost thing to do is to include the header file for creating the dictionary. If you are going to use strings for names or something like that, make sure to include that file as well.
#include<map> #include<string>
02) Creating a Map
Now that you have added the header file, it's time to create the map. We'll create a map where the key is a string(Student Names) and values are integers(their Roll Numbers). We'll name it as Section.
map<string, integer> section;
03) Assigning Values
Thereafter, it's time to assign the values in our Dictionary. Look at the below code snippet:
section["Ananya"]=101; section["Bhavya"]=102; section["Dev"]=103; section["Jay"]=104; section["Om"]=105;
04) Traversing via Loop
Now that you have given entries as well, let's move forward to iterate through the elements. Two methods are used for traversing begin() which points to the beginning of the map and end() which points to the last element of the map.
In the below code snippet, .first accesses the first value of a pair, and .second accesses the second one.
map <string,integer> :: iterator i; cout<<"Keys"<<" & "<<"Value"<<endl; for (i = section.begin(); i!= section.end(); i++) { cout<<(*i).first<<" "<<(*i).second<<"\n"; }
Example of C++ Dictionary
Here is an example for you to try:
#include <iostream> #include <map> #include <string> using namespace std; int main() { map<string, string>CapitalCity;
//Adding the elements CapitalCity["New Delhi"] = "India"; CapitalCity["Bangalore"] = "Karnataka"; CapitalCity["Mumbai"] = "Maharashtra";
//Traversing through the map elements for (auto element :CapitalCity) {
//element.first represents key cout<<element.first<<" is the capital of ";
//element.second represents value cout<<element.second<<endl; } return 0; }
Output:
New Delhi is the capital of India. Bangalore is the capital of Karnataka. Mumbai is the capital of Maharashtra.
Takeaways
Now that you reached the end of this tech blog, we are sure you had fun with this topic. With the step-by-step building of a dictionary in C++ and a whole new example, you must have gained a good amount of experience in this subject.
Other than that, we recommend you to keep practicing this topic because it may seem easy at first but, when you'll have to include it in a project it'll be a tough game! So, keep practicing and for more such tech blogs, keep coming back to FavTutor!