The unordered map serves as an associative container that combines a key value with a mapped value to create elements. Each element is uniquely identified by its key, with the mapped value representing the associated content. Both keys and values can belong to predefined or custom types. Conceptually, an unordered map functions as a dictionary-like data structure that internally stores elements. The key-value pairs it contains facilitate efficient retrieval of individual elements based on their respective keys.
The key provided to the map undergoes hashing to determine the indexes within a hash table. This emphasizes the crucial role of the hash function in dictating the efficiency of the data structure. However, typically, the operations of searching, inserting, and deleting elements from the hash table maintain an average time complexity of O(1).
In the most unfavorable scenario, particularly with sizable prime numbers, the time complexity can vary from O(1) to O(n). It is strongly advised to make use of a map in such instances to prevent encountering a TLE (Time Limit Exceeded) problem.
Syntax:
Unordered_map<int, string>umap
Example:
//A c++ program to check an unordered map in it.
#include <iostream>
#include <unordered_map>
using namespace std;
int main()
{
unordered_map<string, int>umap;
umap["javacpptutorial"] = 20;
umap["regular"] = 30;
umap["distribute"] = 40;
for (auto y :umap)
cout<<y.first<< " " <<
y.second<<endl;
}
Output
Distribute 40
Regular 30
Javacpptutorial 20
Explanation:
This result specifically explains that the output value of an unordered map is produced in a random key-to-value sequence, whereas a map displays the key and value in an ordered manner.
Unordered set vs Unordered map
Some variances between Unordered set and Unordered map include:
Unordered map
- Only (key-value) pairs are found in the elements of an unordered map .
- Use the operator "" to extract a key's corresponding value from a map.
- Key-value pairs are mostly utilised to determine whether a set is present or absent and are not always present in an unordered set.
- Using the find function , an element is searched for. Thus, there is no need for an operator.
Unordered set
Importancpp tutorial:
For example, let's consider the task of calculating the occurrence of each word. Due to the inability to store counts in an unordered set (or set), we need to opt for an unordered map instead.
Map vs. Unordered map
Some variances between the Map and Unordered map are outlined below:
Unordered map
- Any order may be used to store the unordered map key.
- The implementation of unordered map results in an uneven tree structure, making it impossible to retain the order of the entries.
- Operations on an unordered map typically have an o(1) time complexity .
- The map is an ordered list of distinct keys.
- It is possible to preserve the elements' order (by specific tree traversal) because map uses a balanced tree structure.
- The map operations have an o time complexity (log n) .
Procedures for unordered map
There are numerous functions that can be used with unordered map. The ones who are most helpful are:
- Operator =
- Operator
- Beginning and ending of the iterator
- Empty
- Size of the capacity
- For a lookup, locate and count.
- Insert and delete
The complete set of methods available for an unordered map are detailed as follows:
At:
This C++ unordered map function retrieves a reference to the value associated with the specified key element, denoted as k.
Begin:
It offers a result that is an iterator indicating the initial entry in the unordered map container.
End:
The unordered map container's bucket function returns an iterator that points to the position following the last element stored in the container.
Bucket:
It provides the index of the bucket within the map's bucket count where the element associated with the key k is assigned.
Bucket_count
The count of buckets in an unordered map is calculated using the bucket_count method, which can be invoked without any arguments.
Bucket size
It provides the count of elements in the unordered map for each bucket.
Count
It provides the count of elements in each bucket of an unordered map, indicating the number of elements with a specific key in the unordered map that should be tallied within a given range.
Equal_eange
It provides the limits of a span containing all the elements within the container and a key that can be compared to k.
Find
Gives an iterator to the element's empty.
Position
It checks if the unordered map container is devoid of elements.
Erase
Items within the unordered map container can be removed by utilizing the erase method.
Although the capabilities to access the internal bucket size, number of buckets, hash function in use, and different hash policies are available in the c++11 library, they may not be as practical in real-world scenarios. By utilizing iterators, we can iterate through each element stored within the unordered map.
Example:
#include <iostream>
#include <unordered_map>
using namespace std;
int main()
{
// when we will declare a umap it must be of <string, double> type and here the key will be of string type and the mapped value of double in nature
unordered_map<string, double>umap = { //in this we will insert the element in map directly
{"one", 1},
{"two", 2},
{"three", 3}
};
// here wi will insert the values by the help of the [] operator
umap["the value of pi"] = 3.14;
umap["the value of root2"] = 1.414;
umap["the value ofroot3"] = 1.732;
umap["the value oflog10"] = 2.302;
umap["the value ofloge"] = 1.0;
// inserting value by insert function
umap.insert(make_pair("e", 2.718));
string key = "the value of pi";
// if key not found in map iterator
// to end is returned
if (umap.find(key) == umap.end())
cout<< key <<" cannot retrieved\n\n";
// if key found then iterator to that
// key is returned
else
cout<< "retrieved "<< key << "\n\n";
key = "lambda value";
if (umap.find(key) == umap.end())
cout<< key <<" cannot retrieved\n";
else
cout<< "found "<< key <<endl;
// now we will iterate over all value of umap
unordered_map<string, double>::iterator itr;
cout<< "\nthe entire elements : \n";
for (itr = umap.begin();
itr != umap.end(); itr++)
{
cout<<itr->first << " " <<itr->second <<endl;
}
return 0;
}
Output
Retrieved the value of pi
Lambda value cannot retrieved
The entire elements :
E 2.718
The value ofloge 1
The value oflog10 2.302
The value of root2 1.414
The value ofroot3 1.732
The value of pi 3.14
Two 2
Three 3
One 1
Example:
// It is a c++ program to find rhefreqency of it ,in this we will use of unordered_map of every word
#include <bits/stdc++.h>
using namespace std;
void printfrequencies(const string &str)
{
unordered_map<string, int>wordfreq;
stringstream ss(str);
string word;
while (ss>> word)
wordfreq[word]++;
unordered_map<string, int>:: iterator q;
for (q = wordfreq.begin();
q != wordfreq.end(); q++)
cout<< "(" << q->first << ", " <<
q->second << ")\n";
}
int main()
{
string str = "java cpp tutorials questions "
"learn programs";
printfrequencies(str);
return 0;
}
Output
(programs, 1)
(learn, 1)
(questions, 1)
(t, 1)
(points, 1)
(java, 1)