The unordered map is an associated container that holds elements created by fusing a mapped value with a key value. The element is identified specifically by its key value , and the mapped value is the content related to the key. Keys and values may both be of any established or user-defined type . An unordered map can be thought of as a data structure of the dictionary type that stores elements within itself. The sequential pairs it holds (key, value) enable quick retrieval of a specific element using its individual key.
The key supplied to the map is hashed into the indices of a hash table, which is why the speed of the data structure heavily depends on the hash function, but on average, the cost of search, insert, and delete from the hash table is o(1).
In the worst case, especially for large prime integers, its time complexity can range from o(1) to o(n) . It is highly recommended to utilise a map in this case to avoid receiving a tle (time limit exceeded) issue.
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 output specifically justifies the fact that the unordered map's output value is generated in a random key-to-value manner while the map shows value and key in an ordered fashion.
Unordered set vs Unordered map
Some differences between Unordered set and Unordered map are as follows:
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 instance, take the issue of counting the frequency of individual words. Since, counts cannot be stored in unordered set (or set), we must instead use unordered map.
Map vs. Unordered map
Some differences between the Map and Unordered map are as follows:
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 full list of an unordered map's methods is shown below:
At:
This c++ unordered map method returns a reference to the value with the specified element as the key k .
Begin:
It provides a return value that is an iterator pointing to the first entry in the unordered map container.
End:
The unordered map container bucket returns an iterator pointing to the location after the final element .
Bucket:
It returns the bucket number in the map's bucket count where the element with key k is placed.
Bucket_count
The unordered map's total number of buckets is tallied using the bucket count function. It can be called without passing any parameters.
Bucket size
It gives the unordered map count's element count for each bucket .
Count
It gives the unordered map count's element count for each bucket the number of elements in an unordered map with the specified key equal range should be counted.
Equal_eange
It returns the boundaries of a range with all the container's items and a key that compares to k .
Find
Gives an iterator to the element's empty.
Position
It determines whether the unordered map container's container is empty.
Erase
Elements in the unordered map container can be deleted using the erase function.
Although the functions to view the internal bucket size, bucket count, used hash function, and various hash policies are also provided by the c++11 library , they are less helpful in practical applications. Using iterator, we may loop through every element in 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)