Unordered_maps are containers in STL that stores the elements as key-value pairs in any order. Each element has a unique key value. Keys cannot be changed, once they are added to the map, they can only be inserted or deleted, but the values can be altered. They are implemented as hash-table in memory. The map is present in #include<unordered_map> header file. The elements inside the map can be accessed using iterators.
Unordered_map Declaration
Syntax:
unordered_map <key_data_type, value_data_type> name {initial_values};
key_data_type: data type of the key to be stored inside the map
value_data_type: data type of the value to be stored inside the map
initial_values: optional parameter which initializes the map with the given values
Example:
unordered_map <int, string> m; //initializes a unordered_map of size 0 which have key elements as integers and values as strings
unordered_map <int, string> m = {{1, "a"}, {2, "ab"}, {2, "ab"}, {3, "abc"}} //initializes a unordered_map having initial key-value pairs as {1, a}, {2, ab}, {3, abc}Functions on unordered_maps
begin(): Returns an iterator to the first element of the unordered_map.
Parameters: None
Return type: iterator
end(): Returns an iterator to the element past the last element of the unordered_map.
Parameters: None
Return type: iterator
[key]: Returns the value associated with the given key.
Parameters: Key
Return type: value, associated with the key
size(): It tells us the size of the unordered_map.
Parameters: None
Return type: integer - total number of elements in the unordered_map
insert(pair): Insert a pair in the unordered_map.
Parameters: the pair to be inserted
Return type: void
erase(key)orerase(pos_iterator): Delete an element from the unordered_map.
Parameters: the key of the element to be removed or iterator pointing to the position at which the element needs to be deleted
Return type: void
find(key): Returns an iterator pointing to the element, if the key is found else returns an iterator pointing to the end of the unordered_map.
Parameters: the element which needs to be found
Return type: iterator
clear(): It deletes all the elements from the unordered_map
Parameters: None
Return type: void
empty(): It tells us whether the unordered_map is empty or not.
Parameters: None
Return type: Boolean, true if a unordered_map is empty else false
#include<iostream>
#include<unordered_map>
using namespace std;
int main() {
unordered_map <int, string> m;
m.insert(pair <int, string> (1, "a"));
m[2] = "ab";
m[3] = "abc";
m[4] = "abcd";
unordered_map <int, string> ::iterator it;
cout << "Unordered_map contains\n";
for (it = m.begin(); it != m.end(); it++)
cout << it -> first << " " << it -> second << '\n';
cout << '\n';
m.erase(1);
m.erase(m.find(2));
cout << "After erasing element, size of unordered_map is " << m.size() << '\n';
int val = 3;
if (m.find(val) != m.end())
cout << "The unordered_map contains " << val << " as key" << endl;
else
cout << "The unordered_map does not contains " << val << " as key" << endl;
cout << "Elements of unordered_map\n";
for (it = m.begin(); it != m.end(); it++)
cout << it -> first << " " << it -> second << '\n';
cout << '\n';
m.clear();
if (m.empty() == true) {
cout << "Unordered_map is empty now!";
}
return 0;
}Output
Unordered_map contains
4 abcd
3 abc
1 a
2 ab
After erasing element, size of unordered_map is 2
The unordered_map contains 3 as key
Elements of unordered_map
4 abcd
3 abc
Unordered_map is empty now!