A dictionary can be implemented in C++ with the help of std::map of standard template library (STL). It can implement sorted key-value pairs with unique keys. We can alter, search, remove and insert the values associated with the keys in a map within O(N) time complexity. But we can only insert or delete the keys, we cannot alter them.
In this article, we will discuss different ways to create a dictionary in C++ and basic functions which are associated with it.
[toc]
How to create a dictionary in C++?
We can initialize a dictionary using different methods. Let’s understand them:
In C++, the key-value pairs are separated by commas and curly brackets are used for initialization. Methods to create a dictionary are given below:
Code:
#include
#include
#include
Output:
The map language is :
KEY ELEMENT
1 Java
2 Python
3 C++
4 Javascript
The map fruits is :
KEY ELEMENT
1 Banana
2 Mango
3 Cocoa
4 Raspberry
The map cars is :
KEY ELEMENT
1 Honda
2 Toyota
3 Maruti
4 Mercedes
The map map2 is :
KEY ELEMENT
1 Java
2 Python
3 C++
4 Javascript
The map map3 is :
KEY ELEMENT
3 Cocoa
4 Raspberry
Member functions associated with Map in C++:
Modifiers
clear(): It can be used to empty the map.
erase(): It will remove the element which is stored at the iterator position, or value at the given key, or in the range as well.
pair insert(key, value): It is used to add new elements in the map.
swap(map): Exchanges the elements of two containers.
emplace(): It will insert a new key-value pair in the map if there is no equivalent key present in the map before the execution of this function.
Code:
#include
#include
#include
Output:
Map after erase:[ 5:PHP 6:C# 7:Pascal 8:Spring ]
Map after insert:[ 1:Java 2:Python 3:C++ 4:Javascript 5:PHP 6:C# 7:Pascal 8:Spring 9:SQL ]
Map2 conains:[ 1:Java 2:Python 3:C++ 4:Javascript 5:PHP 6:C# 7:Pascal 8:Spring 9:SQL ]
Dictionary contains:[ 1:Honda 2:Mercedes 3:Audi 4:Toyota ]
After emplace:[ 1:Honda 2:Mercedes 3:Audi 4:Toyota 5:Porsche 6:Rolls Royce ]
After Clear:[ ]
Capacity
empty(): It will check if the map is empty
size(): It will return the number of key-value pairs in the map.
max_size(): It will return the maximum number of elements that the map can hold. Example: It can be used to check beforehand whether the map will allow for 1000 elements to be inserted.
Code:
#include
#include
Output:
Size of dictionary(map) is:4
Dictionary can hold: 384307168202282325 values
This map can hold more than 1000 values.
Dictionary is not empty
Iterators
begin(): Returns an iterator to the first element in the map.
end(): Returns an iterator to the last element in the map.
rbegin() It will return a reverse iterator which will be pointing to the last element in the container. Reverse iterator iterates backwards.
rend(): It will return a reverse iterator which will point to the first element in the container.
Code:
#include
#include
#include
Output:
USING BEGIN & END
Elements in dictionary are :
KEY ELEMENT
1 Java
2 Python
3 C++
4 Javascript
USING RBEGIN & REND
Elements in dictionary are :
KEY ELEMENT
4 Javascript
3 C++
2 Python
1 Java
Operations
find(k): It will search for the key x in the container and will return an iterator if found or it will take the iterator to the end.
count(x): It will search for the key x in the container and will return the number of matches.
lower_bound(): It returns an iterator pointing to the first element whose key is not considered to go before ‘x’(it will be either equivalent or greater)
upper _bound():It returns an iterator pointing to the element whose key is considered to go after ‘x’. It returns an iterator pointing to the next element where it finds the equivalent key.
equal_range(): It returns a pair of iterators. Pair means the bounds of a range which will include all the elements in the container which have a key equivalent to ‘x’. Maps only have unique keys, so the first iterator in the pair points to the next key that is after ‘x’.
Code:
#include
#include
#include
Output:
Element 1 Value=Java
Element 2 Value=Python
1 is an element of dictionary.
2 is an element of dictionary.
3 is an element of dictionary.
4 is an element of dictionary.
5 is not an element of dictionary.
6 is not an element of dictionary.
1=Java
Lower bound points to= 2 --> Python
Upper bound points to= 3 --> C++
Conclusion
In this article, we discussed methods to create a dictionary in C++ and its member functions which can be used to perform different types of operations.
That's all about Dictionary in C++.
Happy Learning!!