How to Create a Dictionary in C++?
Last Updated :
15 Oct, 2024
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;
}
Output1: 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;
}
Output2: 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 |
---|
Similar Reads
How to Create a Deque of Maps in C++? In C++, the Standard Template Library (STL) provides a container called deque that allows efficient insertion and deletion operations at both ends of the container. A map is an associative container that stores elements in key-value pairs.In this article, we will learn how to create a deque of maps
2 min read
How to Declare a Map in C++? In C++, maps are associative containers that store elements in a mapped fashion. Each element has a key value and a mapped value. No two mapped values can have the same key values. In this article, we will learn how to declare a map in C++. How to declare a map in C++?In C++, you can declare a map u
2 min read
How to Declare an Array in C++? In C++, an array is a collection of similar data types in which elements are stored in contiguous memory locations. In this article, we will learn how to declare an array in C++. Declaring an Array in C++In C++, we can declare an array by specifying the type of its elements, followed by the name of
2 min read
How to Declare a List in C++? In C++, list is a data structure used to store elements sequentially in non-contiguous memory locations. This container implements doubly linked list which contains pointers to both previous and next elements in the sequence. In this article, we will learn how to declare a list in C++. Declare a Lis
2 min read
How to Create a Set of Sets in C++? In C++, sets are STL containers that store unique elements of the same type in a sorted manner. Sets of sets, also known as nested sets, are collections in which each element of the outer set contains another set as its element. In this article, we will learn how to create a set of sets in C++. Set
2 min read