The map dictionary type is a built-in feature of C++. It functions as a container for values that are indexed by keys, which means that each item in the container is linked to a key. Additionally, every value in a C++ map needs to have the same type. The values and keys in a C++ map are not required to have the same type, but all of the keys in the map must have the same type. A map can be used in C++, but it requires the inclusion of a map header file in the C++ standard library. A map's values can be iterated in a loop to find the matching key, with each iterated item representing a key-value pair.
The following is how a dictionary functions in C++:
- A dictionary type named map exists in C++ and functions similarly to a container for values that are indexed by keys.
- There is a key for every value in the dictionary, which is also known as a map .
- A C++ map requires that all of the values and keys have the same type; however, it is not a need for all of the values and keys to have the same type.
- A map in C++ has to have its header file included in the C++ standard library to be used.
- It is possible to iterate through a map's values in a loop to find the matching key; each iterated item corresponds to a key-value pair.
1. Making a Dictionary in C++ Using the Initializer List Constructor
An initializer list with key-value pairs is used to initialize the dictionaries directly. It may be useful when you have a predetermined set of key-value pairs to start with the dictionary. It offers a clear and effective method for initializing and creating dictionaries in C++.
Code:
Let's take an example to illustrate how to make a dictionary using Initializer List Constructor in C++:
#include <iostream>
#include <unordered_map>
#include <string>
int main()
{
// Creating an unordered_map using the initializer list constructor
std::unordered_map<std::string, int> dictionary = { {"apple", 10}, {"banana", 5}, {"cherry", 15} };
// Accessing values by key
std::cout << "apple: " << dictionary["apple"] << std::endl;
std::cout << "cherry: " << dictionary["cherry"] << std::endl;
return 0;
}
Output:
apple: 10
cherry: 15
2. Use the default constructor to create a dictionary in C++:
An empty dictionary is created using the default constructor, and then elements are added by using the subscript operator . This method allows you to expand the dictionary with as many key-value pairs as you require.
Code:
Let's take an example to illustrate how to make a dictionary using default Constructor in C++:
#include <iostream>
#include <map>
#include <string>
int main()
{
// Creating an empty map
std::map<std::string, int> dictionary;
// Adding elements to the map
dictionary["apple"] = 10;
dictionary["banana"] = 5;
dictionary["cherry"] = 15;
// Accessing values by key
std::cout << "apple: " << dictionary["apple"] << std::endl;
std::cout << "cherry: " << dictionary["cherry"] << std::endl;
return 0;
}
Output:
apple: 10
cherry: 15
3. Use the copy constructor to create a dictionary in C++:
Using a copy constructor, you can copies key-value pairs to the newly initialized object after receiving input from another existing map variable. It is an additional method of creating a new map object. It should be noted that this method can be used again later on in the program execution and does not relocate the current map object.
Code:
Let's take an example to illustrate how to make a dictionary using copy Constructor in C++:
#include <iostream>
#include <map>
#include <string>
int main()
{
// Creating a dictionary
std::map<std::string, int> originalDictionary = {{"apple", 10}, {"banana", 5}, {"cherry", 15}};
// Creating a new dictionary using the copy constructor
std::map<std::string, int> newDictionary(originalDictionary);
// Accessing values by key
std::cout << "apple in new dictionary: " << newDictionary["apple"] << std::endl;
std::cout << "cherry in new dictionary: " << newDictionary["cherry"] << std::endl;
return 0;
}
Output:
apple in new dictionary: 10
cherry in new dictionary: 15
4. Use the range-based constructor to create a dictionary in C++:
One advantage of using the range-based constructor is that it offers a quick and easy way to create dictionaries with a starting set of key-value pairs. This method works especially well if you wish to initialize the dictionary with predetermined data already in it. If you directly provide the contents of the dictionary during creation, it can assist in simplifying the code and make it easier to comprehend.
Code:
Let's take an example to illustrate how to make a dictionary using range-based Constructor in C++:
#include <iostream>
#include <map>
#include <vector>
#include <string>
int main()
{
// Create a vector of key-value pairs
std::vector<std::pair<std::string, int>> keyValuePairs = {
{"apple", 10},
{"banana", 5},
{"cherry", 15}
};
// Create an empty map
std::map<std::string, int> dictionary;
// Use a range-based for loop to insert key-value pairs
for (const auto& pair: keyValuePairs)
{
dictionary.insert(pair);
}
// Accessing values by key
std::cout << "apple: " << dictionary["apple"] << std::endl;
std::cout << "cherry: " << dictionary["cherry"] << std::endl;
return 0;
}
Output:
apple: 10
cherry: 15