In this chapter we shall learn about:
1. unordered_map introduction.
2. unordered_map declaration.
3. unordered_map member function to get the Capacity.
4. unordered_map member function to iterate over elements
5. unordered_map member function for element access
6. unordered_map member function for element lookup
7. unordered_map member function for element modifiers
8. unordered_map member function to get the buckets
9. unordered_map member function for Hash policy
1. unordered_map introduction.
unordered_map are used to store key value pair.
It allows fast retrieval of individual elements on their keys.
In unordered_map key is used to uniquely identify the element
Internally, the elements are not sorted in any particular order with respect to either their key or mapped values.
But they are organized into buckets depending on their hash values.
This allows for fast access to individual elements directly by their key values.
Below header file should be used for unordered_map:
#include <unordered_map>
2. unordered_map declaration.
Below is how you declare an unordered_map:
std::unordered_map<std::int,std::string> mymap;
mymap = {{1,"Canberra"},{2,"Washington"},{3,"Paris"}};
3. unordered_map member function to get the Capacity.
#include <iostream>
#include <unordered_map>
#include <algorithm>
//for more tutorials on C, C++, STL, DS visit www.ProDeveloperTutorial.com
using namespace std;
int main ()
{
std::unordered_map<int,int> first;
std::unordered_map<int,int> second = {{1,10},{2,20},{3,30}};
std::cout << "first " << (first.empty() ? "is empty" : "is not empty" ) << std::endl;
std::cout << "second " << (second.empty() ? "is empty" : "is not empty" ) << std::endl;
std::cout << "second.size() is " << second.size() << std::endl;
std::cout << "second.max_size = " << second.max_size() << std::endl;
return 0;
}
Output:
first is empty
second is not empty
second.size() is 3
second.max_size = 1152921504606846975
4. unordered_map member function to iterate over elements
Below are the member functions to iterate over the elements:
begin : It will return iterator to beginning
end : It will return iterator to end
cbegin : It will return const_iterator to beginning
cend : It will return const_iterator to end
#include <iostream>
#include <unordered_map>
#include <algorithm>
//for more tutorials on C, C++, STL, DS visit www.ProDeveloperTutorial.com
using namespace std;
int main ()
{
std::unordered_map<int,int> first;
std::unordered_map<int,int> second = {{1,10},{2,20},{3,30}};
std::cout << "Printing second map contents using begin():"<<endl;
for ( auto it = second.begin(); it != second.end(); ++it )
std::cout << " " << it->first << ":" << it->second<<endl;
std::cout << std::endl;
std::cout << "Printing second map contents using cbegin():"<<endl;
for ( auto it = second.cbegin(); it != second.cend(); ++it )
std::cout << " " << it->first << ":" << it->second<<endl;
std::cout << std::endl;
return 0;
}
Printing second map contents using begin():
3:30
2:20
1:10
Printing second map contents using cbegin():
3:30
2:20
1:10
5. unordered_map member function for element access
#include <iostream>
#include <unordered_map>
#include <algorithm>
//for more tutorials on C, C++, STL, DS visit www.ProDeveloperTutorial.com
using namespace std;
int main ()
{
std::unordered_map<int,int> first;
std::unordered_map<int,int> second = {{1,10},{2,20},{3,30}};
second[4]= 40; // new element inserted
second[5]= 50; // new element inserted
std::cout << "Printing second map contents:"<<endl;
for ( auto it = second.begin(); it != second.end(); ++it )
std::cout << " " << it->first << ":" << it->second<<endl;
std::cout << std::endl;
second.at(2) = 2000;
second.at(3) = 3000;
std::cout << "Printing second map contents after modifying using \"at\":"<<endl;
for ( auto it = second.begin(); it != second.end(); ++it )
std::cout << " " << it->first << ":" << it->second<<endl;
std::cout << std::endl;
return 0;
}
Output:
Printing second map contents:
5:50
1:10
2:20
3:30
4:40
Printing second map contents after modifying using "at":
5:50
1:10
2:2000
3:3000
4:40
6. unordered_map member function for element lookup
Below are the member functions for element lookup:
find : It will get iterator to element
count : It will count elements with a specific key
equal_range : It will get range of elements with a specific key
#include <iostream>
#include <unordered_map>
#include <algorithm>
//for more tutorials on C, C++, STL, DS visit www.ProDeveloperTutorial.com
using namespace std;
int main ()
{
std::unordered_map<int,int> first;
std::unordered_map<int,int> second = {{1,10},{2,20},{3,30}};
second[4]= 40; // new element inserted
second[5]= 50; // new element inserted
for (auto& x: {5, 6, 7, 1}) {
if (second.count(x)>0)
std::cout << "second has " << x << std::endl;
else
std::cout << "second has no " << x << std::endl;
}
return 0;
}
second has 5
second has no 6
second has no 7
second has 1
7. unordered_map member function for element modifiers
#include <iostream>
#include <unordered_map>
#include <algorithm>
//for more tutorials on C, C++, STL, DS visit www.ProDeveloperTutorial.com
using namespace std;
int main ()
{
std::unordered_map<int,int> first;
std::unordered_map<int,int> second = {{1,10},{2,20},{3,30}};
second[4]= 40; // new element inserted
second[5]= 50; // new element inserted
second.insert ( {{6,6},{7,7}} );
std::cout << "Printing second map contents:"<<endl;
for ( auto it = second.begin(); it != second.end(); ++it )
std::cout << " " << it->first << ":" << it->second<<endl;
std::cout << std::endl;
second.erase ( second.begin() ); // erasing by iterator
second.erase (7); // erasing by key
std::cout << "Printing second map contents after erase:"<<endl;
for ( auto it = second.begin(); it != second.end(); ++it )
std::cout << " " << it->first << ":" << it->second<<endl;
std::cout << std::endl;
return 0;
}
Output:
Printing second map contents:
7:7
6:6
5:50
1:10
2:20
3:30
4:40
Printing second map contents after erase:
6:6
5:50
1:10
2:20
3:30
4:40
8. unordered_map member function to get the buckets
Below are the member functions for buckets lookup:
bucket_count : It will return number of buckets
max_bucket_count : It will return maximum number of buckets
bucket_size : It will return bucket size
bucket : Locate element’s bucket
#include <iostream>
#include <unordered_map>
#include <algorithm>
//for more tutorials on C, C++, STL, DS visit www.ProDeveloperTutorial.com
using namespace std;
int main ()
{
std::unordered_map<int,int> first;
std::unordered_map<int,int> second = {{1,10},{2,20},{3,30}};
second[4]= 40; // new element inserted
second[5]= 50; // new element inserted
second.insert ( {{6,6},{7,7}} );
unsigned n = second.bucket_count();
std::cout << "second has " << n << " buckets.\n";
for (unsigned i=0; i<n; ++i)
{
std::cout << "bucket #" << i << " size is " << second.bucket_size(i)<< " . It contains: ";
for (auto it = second.begin(i); it!=second.end(i); ++it)
std::cout << " " <<it->first << " "<< it->second;
std::cout << "\n";
}
return 0;
}
Output:
second has 11 buckets.
bucket #0 size is 0 . It contains:
bucket #1 size is 1 . It contains: 1 10
bucket #2 size is 1 . It contains: 2 20
bucket #3 size is 1 . It contains: 3 30
bucket #4 size is 1 . It contains: 4 40
bucket #5 size is 1 . It contains: 5 50
bucket #6 size is 1 . It contains: 6 6
bucket #7 size is 1 . It contains: 7 7
bucket #8 size is 0 . It contains:
bucket #9 size is 0 . It contains:
bucket #10 size is 0 . It contains:
9. unordered_map member function for Hash policy
Below are the member functions for Hash policy:
load_factor : It will return load factor [load_factor = size / bucket_count ]
max_load_factor : It will get or set maximum load factor
rehash : It will set number of buckets
reserve : It will request a capacity change