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

  • How to initialize an array in Constructor in C++
    23 April

    How to initialize an Array in Constructor in C++

    Table of ContentsArraysConstructors of a ClassWays to initialize an array in Constructor in C++Initialize an array in Constructor With std::fill()Initialize an array in Constructor Using for LoopInitialize an array in Constructor Using Member Initializer ListConclusion This article discusses methods of initializing an array in constructor in C++. Let us first discuss arrays and constructors in […]

  • Write String to file in C++
    31 March

    Write String to File in C++

    Table of ContentsUsing the fstream Library to write String to File in C++Using the Insertion Operator (<<) With the fstream ObjectUsing the write() Function of the fstream LibraryUsing the fwrite() Function to write String to File in C++Conclusion The files are used to store the data permanently on a disk. This mitigates the problem of […]

  • Print Array in C++
    23 March

    Print Array in C++

    Table of ContentsIntroduction of ArraysDifferent Methods to Print an Array in C++print array in C++ by Traversing Indicesprint array in C++ Using for_each() Functionprint array in C++ Using Range-Based for Loopprint array in C++ Using Iteratorsprint array Using ostream_iterator() Function and copy() Functionprint array Using C++17 copy() Function and make_ostream_joiner() FunctionConclusion An array is a […]

  • Print Vector C++
    23 March

    Print Vector in C++

    Table of ContentsPrint Vector in C++ Using IndicesPrint Vector in C++ Using Range-Based for LoopPrint Vector in C++ Using for_each() FunctionPrint Vector in C++ Using IteratorPrint Vector in C++ by Overloading the Insertion Operator (<<)Print Vector in C++ Using copy() FunctionConclusion This article discusses the vector and how to print vector in C++. Vectors are […]

  • Print Map in C++
    15 March

    Print Map in C++

    Table of ContentsMap vs Unordered MapPrint Map in C++ Using IteratorPrint Map in C++ Using Range-Based for LoopPrint Map in C++ by Overloading the ostream Insertion Operator (<<)Print Map in C++17 Using Structure Binding and Range-Based for LoopPrint Map in C++17 Using for_each() FunctionConclusionReferences Maps in C++ store the data in form of key and […]

  • How to write binary files in C++
    14 March

    Write Binary Files in C++

    Table of ContentsOpening a File in Binary Mode Using fstream LibraryWriting the Binary File Using the write() Function of fstream LibraryWriting the Binary File Using the put() Function of fstream LibraryWriting the Binary File Using Insertion Operator (<<) Of fstream LibraryWriting the Structure Object to Binary File Using fstream LibraryConclusion In this post, we will […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.