From 1e67a8e9b0659b4fd6491df6b802dd9504d01c3b Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Sat, 2 Aug 2025 17:05:08 +0530 Subject: [PATCH 1/6] [Entry] C++ unordered map: max_load_factor() --- .../terms/max_load-factor/max_load-factor.md | 112 ++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 content/cpp/concepts/unordered-map/terms/max_load-factor/max_load-factor.md diff --git a/content/cpp/concepts/unordered-map/terms/max_load-factor/max_load-factor.md b/content/cpp/concepts/unordered-map/terms/max_load-factor/max_load-factor.md new file mode 100644 index 00000000000..6c820430cb3 --- /dev/null +++ b/content/cpp/concepts/unordered-map/terms/max_load-factor/max_load-factor.md @@ -0,0 +1,112 @@ +--- +Title: 'max_load_factor()' +Description: 'Gets or sets the maximum average number of elements per bucket before rehashing occurs in an unordered_map.' +Subjects: + - 'Computer Science' + - 'Data Science' +Tags: + - 'Map' + - 'STL' +CatalogContent: + - 'learn-c-plus-plus' + - 'paths/computer-science' +--- + +The **`unordered_map::max_load_factor`** function in C++ is used to get or set the maximum load factor of an `unordered_map`. The load factor represents the average number of elements per bucket (i.e., total elements divided by total buckets). By default, this value is set to `1.0`. + +## Syntax + +```cpp +unordered_mapName.max_load_factor(); // Getter +unordered_mapName.max_load_factor(value); // Setter +``` + +**Parameters:** + +- `value` (optional): A `float` specifying the new maximum load factor. Must be greater than 0. + +**Return value:** + +Returns the current maximum load factor as a `float`. + +## Example 1: Getting the default load factor + +This example prints the default max load factor of an unordered map: + +```cpp +#include +#include + +int main() { + std::unordered_map myMap; + std::cout << "Default max load factor: " << myMap.max_load_factor() << std::endl; + return 0; +} +``` + +The output of this code is: + +```shell +Default max load factor: 1 +``` + +This shows that the default load factor is typically `1.0`, meaning the map aims for one element per bucket. + +## Example 2: Setting a custom load factor to reduce rehashing + +This example increases the max load factor to reduce the frequency of rehashing during insertions: + +```cpp +#include +#include + +int main() { + std::unordered_map data; + data.max_load_factor(2.5); + + std::cout << "New max load factor: " << data.max_load_factor() << std::endl; + return 0; +} +``` + +The output of this code is: + +```shell +New max load factor: 2.5 +``` + +Raising the load factor allows more elements per bucket, delaying rehashing and potentially improving insertion performance. + +## Codebyte Example: Lowering load factor to improve search speed + +This example sets a lower load factor to prioritize faster lookups in a time-critical application: + +```codebye/cpp +#include +#include + +int main() { + std::unordered_map wordCount; + wordCount.max_load_factor(0.5); + + wordCount["optimize"] = 1; + wordCount["speed"] = 2; + + std::cout << "Load factor set for quick access: " << wordCount.max_load_factor() << std::endl; + return 0; +} +``` + +## Frequently asked questions + +### 1. What is the default `max_load_factor` for an unordered_map? + +It's usually `1.0`, meaning one element per bucket on average before rehashing is triggered. + +### 2. Does increasing the load factor make the map faster? + +It can speed up insertions by reducing rehashes, but may slow down lookups due to more collisions. + +### 3. What happens if I set the load factor too low? + +The map will rehash more often, using more memory and slowing down insertions, but lookups may become faster. From b6b2bc5e3070fb31fafaed5d88aef639e4abce7c Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Wed, 6 Aug 2025 14:22:41 +0530 Subject: [PATCH 2/6] Update max_load-factor.md --- .../unordered-map/terms/max_load-factor/max_load-factor.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/content/cpp/concepts/unordered-map/terms/max_load-factor/max_load-factor.md b/content/cpp/concepts/unordered-map/terms/max_load-factor/max_load-factor.md index 6c820430cb3..fa32b2e49af 100644 --- a/content/cpp/concepts/unordered-map/terms/max_load-factor/max_load-factor.md +++ b/content/cpp/concepts/unordered-map/terms/max_load-factor/max_load-factor.md @@ -17,8 +17,8 @@ The **`unordered_map::max_load_factor`** function in C++ is used to get or set t ## Syntax ```cpp -unordered_mapName.max_load_factor(); // Getter -unordered_mapName.max_load_factor(value); // Setter +unordered_mapName.max_load_factor(); // Getter +unordered_mapName.max_load_factor(value); // Setter ``` **Parameters:** From 37c404f258bd709f0fbf1c11dd6a594f6617324d Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Wed, 6 Aug 2025 14:24:53 +0530 Subject: [PATCH 3/6] Update max_load-factor.md --- .../terms/max_load-factor/max_load-factor.md | 22 ++++++++++++------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/content/cpp/concepts/unordered-map/terms/max_load-factor/max_load-factor.md b/content/cpp/concepts/unordered-map/terms/max_load-factor/max_load-factor.md index fa32b2e49af..b5e29c2ce24 100644 --- a/content/cpp/concepts/unordered-map/terms/max_load-factor/max_load-factor.md +++ b/content/cpp/concepts/unordered-map/terms/max_load-factor/max_load-factor.md @@ -23,7 +23,7 @@ unordered_mapName.max_load_factor(value); // Setter **Parameters:** -- `value` (optional): A `float` specifying the new maximum load factor. Must be greater than 0. +* `value` (optional): A `float` specifying the new maximum load factor. Must be greater than 0. **Return value:** @@ -37,9 +37,11 @@ This example prints the default max load factor of an unordered map: #include #include +using namespace std; + int main() { - std::unordered_map myMap; - std::cout << "Default max load factor: " << myMap.max_load_factor() << std::endl; + unordered_map myMap; + cout << "Default max load factor: " << myMap.max_load_factor() << endl; return 0; } ``` @@ -60,11 +62,13 @@ This example increases the max load factor to reduce the frequency of rehashing #include #include +using namespace std; + int main() { - std::unordered_map data; + unordered_map data; data.max_load_factor(2.5); - std::cout << "New max load factor: " << data.max_load_factor() << std::endl; + cout << "New max load factor: " << data.max_load_factor() << endl; return 0; } ``` @@ -85,21 +89,23 @@ This example sets a lower load factor to prioritize faster lookups in a time-cri #include #include +using namespace std; + int main() { - std::unordered_map wordCount; + unordered_map wordCount; wordCount.max_load_factor(0.5); wordCount["optimize"] = 1; wordCount["speed"] = 2; - std::cout << "Load factor set for quick access: " << wordCount.max_load_factor() << std::endl; + cout << "Load factor set for quick access: " << wordCount.max_load_factor() << endl; return 0; } ``` ## Frequently asked questions -### 1. What is the default `max_load_factor` for an unordered_map? +### 1. What is the default `max_load_factor` for an unordered\_map? It's usually `1.0`, meaning one element per bucket on average before rehashing is triggered. From e792d4c3b59e62b3b162c5e275313c27f36782fd Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Wed, 6 Aug 2025 14:25:23 +0530 Subject: [PATCH 4/6] Update max_load-factor.md --- .../unordered-map/terms/max_load-factor/max_load-factor.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/cpp/concepts/unordered-map/terms/max_load-factor/max_load-factor.md b/content/cpp/concepts/unordered-map/terms/max_load-factor/max_load-factor.md index b5e29c2ce24..72c70177223 100644 --- a/content/cpp/concepts/unordered-map/terms/max_load-factor/max_load-factor.md +++ b/content/cpp/concepts/unordered-map/terms/max_load-factor/max_load-factor.md @@ -1,6 +1,6 @@ --- Title: 'max_load_factor()' -Description: 'Gets or sets the maximum average number of elements per bucket before rehashing occurs in an unordered_map.' +Description: 'Gets or sets the maximum average number of elements per bucket before rehashing occurs in an unordered map.' Subjects: - 'Computer Science' - 'Data Science' From 246cc1a36d61ed15aeef279953097f921cd38651 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Wed, 6 Aug 2025 14:29:55 +0530 Subject: [PATCH 5/6] format fix --- .../unordered-map/terms/max_load-factor/max_load-factor.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/content/cpp/concepts/unordered-map/terms/max_load-factor/max_load-factor.md b/content/cpp/concepts/unordered-map/terms/max_load-factor/max_load-factor.md index 72c70177223..9bf356a7940 100644 --- a/content/cpp/concepts/unordered-map/terms/max_load-factor/max_load-factor.md +++ b/content/cpp/concepts/unordered-map/terms/max_load-factor/max_load-factor.md @@ -23,7 +23,7 @@ unordered_mapName.max_load_factor(value); // Setter **Parameters:** -* `value` (optional): A `float` specifying the new maximum load factor. Must be greater than 0. +- `value` (optional): A `float` specifying the new maximum load factor. Must be greater than 0. **Return value:** @@ -105,7 +105,7 @@ int main() { ## Frequently asked questions -### 1. What is the default `max_load_factor` for an unordered\_map? +### 1. What is the default `max_load_factor` for an unordered_map? It's usually `1.0`, meaning one element per bucket on average before rehashing is triggered. From 5042a5132b687e241f942630776b7f0838cdac31 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Wed, 6 Aug 2025 14:33:40 +0530 Subject: [PATCH 6/6] renamed the file names to fix error --- .../max_load-factor.md => max-load-factor/max-load-factor.md} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename content/cpp/concepts/unordered-map/terms/{max_load-factor/max_load-factor.md => max-load-factor/max-load-factor.md} (99%) diff --git a/content/cpp/concepts/unordered-map/terms/max_load-factor/max_load-factor.md b/content/cpp/concepts/unordered-map/terms/max-load-factor/max-load-factor.md similarity index 99% rename from content/cpp/concepts/unordered-map/terms/max_load-factor/max_load-factor.md rename to content/cpp/concepts/unordered-map/terms/max-load-factor/max-load-factor.md index 9bf356a7940..d391e98e2ae 100644 --- a/content/cpp/concepts/unordered-map/terms/max_load-factor/max_load-factor.md +++ b/content/cpp/concepts/unordered-map/terms/max-load-factor/max-load-factor.md @@ -1,5 +1,5 @@ --- -Title: 'max_load_factor()' +Title: 'max-load-factor()' Description: 'Gets or sets the maximum average number of elements per bucket before rehashing occurs in an unordered map.' Subjects: - 'Computer Science'