Unordered_multimap max_load_factor() function in C++
The Standard Template Library (STL) in C++ contains a container called std::unordered_multimap that stores elements unorderedly so that they can be efficiently retrieved using the keys. The maximum load factor of the container can be adjusted or obtained using the max_load_factor() function, which is a member function of std::unordered_multimap. The ratio of the number of items in the container to the number of buckets is known as the load factor, and it indicates how full the container is. Although a lower load factor can improve performance in terms of space efficiency, it may also increase the likelihood of collisions. A lower load factor indicates that the container is less filled. The container is bigger with a greater load factor, which could result in fewer collisions, but it also needs more RAM.
Methods used are:
Getting the Maximum Load Factor:
float maxLoadFactor = myUnorderedMultimap.max_load_factor();
It retrieves the current maximum load factor of the unordered_multimap named myUnorderedMultimap.
Setting the Maximum Load Factor:
myUnorderedMultimap.max_load_factor(0.75);
The interaction between space efficiency and collision probability can be regulated by varying the maximum load factor. Although implementation-specific, the C++ standard library typically sets the maximum load factor's default value to a respectable one. You frequently modify the load factor to optimize the unordered_multimap's speed for your particular use case and the properties of the data you are storing.
Example:
Let's take an example to illustrate the use of std::unordered_multimap max_load_factor() in C++.
#include <iostream>
#include <unordered_map>
int main() {
// Create an unordered_multimap of integers with strings as keys
std::unordered_multimap<int, std::string> myUnorderedMultimap;
// Insert some key-value pairs into the unordered_multimap
myUnorderedMultimap.insert({1, "POINT"});
myUnorderedMultimap.insert({2, "JAVA"});
myUnorderedMultimap.insert({3, "HELLO"});
myUnorderedMultimap.insert({1, "T"}); // Duplicate key
// Display the current maximum load factor
float currentLoadFactor = myUnorderedMultimap.max_load_factor();
std::cout << "Current Maximum Load Factor: " << currentLoadFactor << std::endl;
// Set a new maximum load factor
myUnorderedMultimap.max_load_factor(0.75);
// Display the updated maximum load factor
currentLoadFactor = myUnorderedMultimap.max_load_factor();
std::cout << "Updated Maximum Load Factor: " << currentLoadFactor << std::endl;
// Iterate through the unordered_multimap and print its contents
std::cout << "Contents of the Unordered Multimap:" << std::endl;
for (const auto& pair : myUnorderedMultimap) {
std::cout << "Key: " << pair.first << ", Value: " << pair.second << std::endl;
}
return 0;
}
Output:
Current Maximum Load Factor: 1
Updated Maximum Load Factor: 0.75
Contents of the Unordered Multimap:
Key: 3, Value: HELLO
Key: 2, Value: JAVA
Key: 1, Value: T
Key: 1, Value: POINT
Example 2:
Let's take another example to illustrate the use of std::unordered_multimap max_load_factor() in C++.
#include <iostream>
#include <unordered_map>
int main() {
// Create an unordered_multimap of characters with integers as values
std::unordered_multimap<char, int> myUnorderedMultimap;
// Insert some key-value pairs into the unordered_multimap
myUnorderedMultimap.insert({'a', 1});
myUnorderedMultimap.insert({'b', 2});
myUnorderedMultimap.insert({'c', 3});
myUnorderedMultimap.insert({'a', 4}); // Duplicate key
// Display the current maximum load factor
float currentLoadFactor = myUnorderedMultimap.max_load_factor();
std::cout << "Current Maximum Load Factor: " << currentLoadFactor << std::endl;
// Set a new maximum load factor
myUnorderedMultimap.max_load_factor(0.8);
// Display the updated maximum load factor
currentLoadFactor = myUnorderedMultimap.max_load_factor();
std::cout << "Updated Maximum Load Factor: " << currentLoadFactor << std::endl;
// Iterate through the unordered_multimap and print its contents
std::cout << "Contents of the Unordered Multimap:" << std::endl;
for (const auto& pair : myUnorderedMultimap) {
std::cout << "Key: " << pair.first << ", Value: " << pair.second << std::endl;
}
return 0;
}
Output:
Current Maximum Load Factor: 1
Updated Maximum Load Factor: 0.8
Contents of the Unordered Multimap:
Key: c, Value: 3
Key: b, Value: 2
Key: a, Value: 4
Key: a, Value: 1
Example 3:
Let's take another example to illustrate the use of std::unordered_multimap max_load_factor() in C++.
#include <iostream>
#include <unordered_map>
int main() {
// Create an unordered_multimap of strings with doubles as values
std::unordered_multimap<std::string, double> myUnorderedMultimap;
// Insert some key-value pairs into the unordered_multimap
myUnorderedMultimap.insert({"apple", 2.5});
myUnorderedMultimap.insert({"banana", 1.8});
myUnorderedMultimap.insert({"orange", 3.2});
myUnorderedMultimap.insert({"apple", 4.1}); // Duplicate key
// Display the current maximum load factor
float currentLoadFactor = myUnorderedMultimap.max_load_factor();
std::cout << "Current Maximum Load Factor: " << currentLoadFactor << std::endl;
// Set a new maximum load factor
myUnorderedMultimap.max_load_factor(0.6);
// Display the updated maximum load factor
currentLoadFactor = myUnorderedMultimap.max_load_factor();
std::cout << "Updated Maximum Load Factor: " << currentLoadFactor << std::endl;
// Iterate through the unordered_multimap and print its contents
std::cout << "Contents of the Unordered Multimap:" << std::endl;
for (const auto& pair : myUnorderedMultimap) {
std::cout << "Key: " << pair.first << ", Value: " << pair.second << std::endl;
}
return 0;
}
Output:
Current Maximum Load Factor: 1
Updated Maximum Load Factor: 0.6
Contents of the Unordered Multimap:
Key: orange, Value: 3.2
Key: banana, Value: 1.8
Key: apple, Value: 2.5
Key: apple, Value: 4.1
Conclusion:
Developers can effectively regulate the trade-off between memory efficiency and lookup performance with the help of the C++ max_load_factor() method for the std::unordered_multimap container. Through the manipulation of the maximum load factor, developers can optimize the element density inside the container, impacting variables like collision probabilities and space utilization. Although less memory is used when the maximum load factor is lower, there is a chance of more collisions, which could affect performance. While a higher maximum load factor can reduce collisions, it may also result in higher memory utilization. Programmers can tailor the behaviour of std::unordered_multimap to their specific application requirements and data characteristics by using the flexibility provided by the max_load_factor() function. It allows for a balance that is specifically tailored to each scenario's needs.