Dictionary in C++

Dictionary in C++

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.

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:

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:

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:

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:

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:

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!!

Related Posts

  • Trim String in C++
    19 July

    Trim String in C++

    Table of ContentsUsing Boost string algorithms to trim string in C++Using find_first_not_of() and find_last_not_of()` to trim strings in C++Using find_if() to trim the string in C++Using stringstream to trim the strings in C++Using a customized function to trim the strings in C++Conclusion When we take an input from an user, strings can have unwanted whitespaces […]

  • JSON parser in C++
    19 July

    JSON Parser in C++

    Table of ContentsSimple JSON Parser in C++ using JsonCpp libraryConclusion In this post, we will see about JSON parser in C++. There is no native support for JSON in C++. We can use a number of libraries that provide support for JSON in C++. We will use JsonCpp to parse JSON files in C++ which […]

  • Enum to String in C++
    19 July

    Convert enum to string in C++

    Table of ContentsUsing stringify macro method to convert enum to String in C++Using const char* Array to convert enum to String in C++Using a custom-defined function to convert enum to String in C++Conclusion There are a variety of methods to convert enum to String in C++. In this article, we will discuss some of the […]

  • C++ random number between 0 and 1
    11 June

    C++ random number between 0 and 1

    Table of ContentsUsing the rand() function to generate random number between 0 and 1 in C++Using the std::uniform_real_distribution() function to generate random number between 0 and 1 in C++ Random numbers are frequently used in programming for test case generation, cryptography, and other related purposes. In this tutorial, we will generate random number between 0 […]

  • String contains C++
    07 June

    Check if string contains substring in C++

    Table of ContentsUsing find() function to check if string contains substring in C++.Using strstr() method to check if string contains substring in C++Using boost library to check if string contains substring in C++Using custom functions to check if string contains substring in C++Conclusion There are different ways to check if string contains substring in C++. […]

  • Convert float to String in C++
    24 May

    Convert float to String in C++

    Learn about how to convert float to String in C++ in different wayds.

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.