Open In App

How to Create a Dictionary in C++?

Last Updated : 15 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

A dictionary is a data structure in python that enables users to store data in the form of key-value pairs, where each key must be unique. In this article, we will learn how to create a dictionary-like data structure in C++.

Examples

Input: {{1 : "C++"}, {3: "Python"}, {2: "Java"}}
Output: 1 : C++
2 : Python
3 : Java
Explanation: Sorted order dictionary is created using std::map

Input: {{45 : "Geeks"}, {11 : "For" }, {9: "Geeks"}}
Output: 9 : Geeks
11 : For
45 : Geeks
Explanation: Sorted order dictionary is created using std::map

In C++, there are two data structures that allows users to store data in the form of key value pairs: map and unordered_map. So, we can implement the dictionary-like data structure using two different ways in C++:

Using std::map

We can use the std::mapto create the dictionary in C++. A map is a container from the Standard Template Library (STL) that stores key-value pairs in ascending order of key. As C++ is statically typed language, we have to declare the type of key and value while creating a map.

Syntax to create std::map

std::map<key_type, value_type> name;

Example

C++
// C++ Program to Create a Dictionary 
// using std::map
#include <bits/stdc++.h>
using namespace std;

int main() {

    // Create an empty dictionary using std::map
    map<int, string> dict;

    // Insert key-value pairs
    dict[1] = "C++";
    dict[3] = "Python";
    dict[2] = "Java";

    for (auto it : dict)
        cout << it.first << ": " << it.second << endl;

    return 0;
}

Output
1: C++
2: Java
3: Python

In C++, std::map is implemented as a balanced binary tree (typically a Red-Black Tree) so it is a bit less efficient than the python dictionaries. But

Using std::unordered_map

STLstd::unordered_map container provides the closest inbuilt thing you can to the Python dictionary in C++. It stores the data in key-value pair. It internally uses a hash table for implementation, so it is as fast as dictionary.

Syntax to create std::unordered_map

std::unordered_map<key_type, value_type> name;

Example

C++
// C++ Program to Create a dictionary using
// unordered_map
#include <bits/stdc++.h>
using namespace std;

int main() {

    // Create an empty dictionary using unordered_map
    unordered_map<int, string> dict;

    // Insert key-value pairs
    dict[1] = "C++";
    dict[3] = "Python";
    dict[2] = "Java";

    for (auto it : dict)
        cout << it.first << ": " << it.second << endl;

    return 0;
}

Output
2: Java
3: Python
1: C++

Time Complexity: O(1), worst case O(n), where n is the number of keys
Auxiliary Space: O(n)

C++ map vs unordered_map vs Python Dictionary

The following table lists the major differences between C++ map, C++ unordered_map and Python Dictionary:

Feature

C++ map

C++ Unordered Map

Python Dictionary

Implementation

Balanced binary tree (usually Red-Black Tree)

Hash table

Hash table

Ordering

Sorted (by key)

Unordered

Insertion-ordered

Time Complexity (Search, Insert, Delete)

O(log N)

O(1) on average, O(N) in worst case

O(1) on average, O(N) in worst case

Duplicate Keys

Not allowed

Not allowed

Not allowed

Available in

C++ (STL)

C++ (STL)

Python



Next Article
Article Tags :
Practice Tags :

Similar Reads