From f6d3d3d498acad7770c7cb8a82eb9fa813007337 Mon Sep 17 00:00:00 2001 From: Ali Nasser Date: Mon, 12 Jan 2026 23:05:01 +0200 Subject: [PATCH 1/5] Document max_load_factor() method for unordered_set Added documentation for the max_load_factor() method in unordered_set. --- .../terms/max-load-factor/max-load-factor.md | 107 ++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 content/cpp/concepts/unordered-set /terms/max-load-factor/max-load-factor.md diff --git a/content/cpp/concepts/unordered-set /terms/max-load-factor/max-load-factor.md b/content/cpp/concepts/unordered-set /terms/max-load-factor/max-load-factor.md new file mode 100644 index 00000000000..736051eb903 --- /dev/null +++ b/content/cpp/concepts/unordered-set /terms/max-load-factor/max-load-factor.md @@ -0,0 +1,107 @@ +--- +Title: 'max_load_factor()' +Description: 'Get or set the maximum load factor for the unordered set' +Subjects: + - 'Code Foundations' + - 'Computer Science' +Tags: + - 'Methods' + - 'Sets' + - 'STL' +CatalogContent: + - 'learn-c-plus-plus' + - 'paths/computer-science' +--- + +The **`max_load_factor()`** method has two forms: + +1- Does not accept any parameters and returns the current maximum load factor of the [`unordered_set`](https://www.codecademy.com/resources/docs/cpp/unordered-set). + +2- Accepts one parameter and sets it as the new maximum load factor, and does not return any values. + +The load factor is the ratio between the number of container elements and the number of container buckets (size / bucket_count). +If two elements are located in the same bucket, it may cause a collision in the hash table of the container. That's why the container uses the max load factor as the threshold that forces an increase in the number of buckets and thus causes a rehash. + +By default, the maximum load factor of an unordered set is 1.0. + + +## Syntax + +```pseudo +unordered_set_name.max_load_factor(); // returns the maximum load factor + +unordered_set_name.max_load_factor(double z); // sets z as the maximum load factor +``` + +## Example + +This example demonstrates how to read and modify the maximum load factor and how it affects the number of buckets in the container. +Most standard library implementations pre-allocate a relatively large number of buckets, so `rehash()` is used here to force the container to start small and make the effect visible. + +```cpp +#include +#include + +int main() { + std::unordered_set s; + std::cout << "Initial maximum load factor: " << s.max_load_factor() << "\n"; + s.rehash(1); // Forces the container to start with 1 bucket + s.max_load_factor(0.5f); + + std::cout << "Initial buckets: " << s.bucket_count() << "\n\n"; + + s.insert(5); + + std::cout << "After inserting 5:\n"; + std::cout << "max_load_factor = " << s.max_load_factor() << "\n"; + std::cout << "size = " << s.size() << "\n"; + std::cout << "bucket_count = " << s.bucket_count() << "\n"; + std::cout << "load_factor = " << s.load_factor() << "\n"; +} + +``` +Even though `rehash(1)` was called, the insertion caused the container to increase the number of buckets to satisfy the constraint: + +`size / bucket_count <= max_load_factor` + +Since `max_load_factor = 0.5`, at least two buckets are required for one element, and the implementation chose 23. + +Output of this code: + +```shell +Initial maximum load factor: 1 +Initial buckets: 2 + +After inserting 5: +max_load_factor = 0.5 +size = 1 +bucket_count = 23 +load_factor = 0.0434783 +``` + +## Codebyte Example + +Test and modify the code below: + +```codebyte/cpp +#include +#include + +int main() { + std::unordered_set s; + std::cout << "Initial maximum load factor: " << s.max_load_factor() << "\n"; + s.rehash(1); // Forces the container to start with 1 bucket + s.max_load_factor(0.5f); + + std::cout << "Initial buckets: " << s.bucket_count() << "\n\n"; + + s.insert(5); + + std::cout << "After inserting 5:\n"; + std::cout << "max_load_factor = " << s.max_load_factor() << "\n"; + std::cout << "size = " << s.size() << "\n"; + std::cout << "bucket_count = " << s.bucket_count() << "\n"; + std::cout << "load_factor = " << s.load_factor() << "\n"; +} + +``` From 69ea09a053c6ca2977e50777be624619a05a9b9a Mon Sep 17 00:00:00 2001 From: Ali Nasser Date: Mon, 12 Jan 2026 23:24:28 +0200 Subject: [PATCH 2/5] Rename max-load-factor.md to max-load-factor.md --- .../terms/max-load-factor/max-load-factor.md | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename content/cpp/concepts/{unordered-set => unordered-set}/terms/max-load-factor/max-load-factor.md (100%) diff --git a/content/cpp/concepts/unordered-set /terms/max-load-factor/max-load-factor.md b/content/cpp/concepts/unordered-set/terms/max-load-factor/max-load-factor.md similarity index 100% rename from content/cpp/concepts/unordered-set /terms/max-load-factor/max-load-factor.md rename to content/cpp/concepts/unordered-set/terms/max-load-factor/max-load-factor.md From e8a97a48d1a5b9ad78f1273d1718be3c270cefde Mon Sep 17 00:00:00 2001 From: Ali Nasser Date: Mon, 12 Jan 2026 23:31:53 +0200 Subject: [PATCH 3/5] Improve clarity of max_load_factor method documentation Clarified the description of the max_load_factor method and its behavior regarding load factor and hash collisions. --- .../terms/max-load-factor/max-load-factor.md | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/content/cpp/concepts/unordered-set/terms/max-load-factor/max-load-factor.md b/content/cpp/concepts/unordered-set/terms/max-load-factor/max-load-factor.md index 736051eb903..e616cfdf80b 100644 --- a/content/cpp/concepts/unordered-set/terms/max-load-factor/max-load-factor.md +++ b/content/cpp/concepts/unordered-set/terms/max-load-factor/max-load-factor.md @@ -15,14 +15,16 @@ CatalogContent: The **`max_load_factor()`** method has two forms: -1- Does not accept any parameters and returns the current maximum load factor of the [`unordered_set`](https://www.codecademy.com/resources/docs/cpp/unordered-set). +1. A version that takes no arguments and returns the current maximum load factor of the [`unordered_set`](https://www.codecademy.com/resources/docs/cpp/unordered-set). -2- Accepts one parameter and sets it as the new maximum load factor, and does not return any values. +2. A version that takes one argument and sets it as the new maximum load factor. This version does not return a value. -The load factor is the ratio between the number of container elements and the number of container buckets (size / bucket_count). -If two elements are located in the same bucket, it may cause a collision in the hash table of the container. That's why the container uses the max load factor as the threshold that forces an increase in the number of buckets and thus causes a rehash. +The **load factor** is defined as: +`load_factor = number_of_elements / number_of_buckets` -By default, the maximum load factor of an unordered set is 1.0. +If multiple elements are placed into the same bucket, hash collisions may occur. To limit this, the container uses the **maximum load factor** as a threshold. When inserting an element would cause the load factor to exceed this value, the container automatically increases the number of buckets and performs a **rehash**. + +By default, the maximum load factor of an `unordered_set` is **1.0**. ## Syntax From e32a04731d344f64149f7a1957b66e3929748e62 Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Tue, 13 Jan 2026 13:17:29 +0530 Subject: [PATCH 4/5] Clarify max_load_factor method and update examples Updated the description of the max_load_factor method and clarified its functionality. Enhanced example code to demonstrate setting and modifying the maximum load factor. --- .../terms/max-load-factor/max-load-factor.md | 89 +++++++++---------- 1 file changed, 44 insertions(+), 45 deletions(-) diff --git a/content/cpp/concepts/unordered-set/terms/max-load-factor/max-load-factor.md b/content/cpp/concepts/unordered-set/terms/max-load-factor/max-load-factor.md index e616cfdf80b..2c92421892f 100644 --- a/content/cpp/concepts/unordered-set/terms/max-load-factor/max-load-factor.md +++ b/content/cpp/concepts/unordered-set/terms/max-load-factor/max-load-factor.md @@ -1,6 +1,6 @@ --- Title: 'max_load_factor()' -Description: 'Get or set the maximum load factor for the unordered set' +Description: 'Gets or sets the maximum load factor of the unordered set.' Subjects: - 'Code Foundations' - 'Computer Science' @@ -13,28 +13,27 @@ CatalogContent: - 'paths/computer-science' --- -The **`max_load_factor()`** method has two forms: +The **`max_load_factor()`** method gets or sets the maximum load factor of an `unordered_set`. The load factor is defined as the ratio of the number of elements to the number of buckets: -1. A version that takes no arguments and returns the current maximum load factor of the [`unordered_set`](https://www.codecademy.com/resources/docs/cpp/unordered-set). - -2. A version that takes one argument and sets it as the new maximum load factor. This version does not return a value. - -The **load factor** is defined as: `load_factor = number_of_elements / number_of_buckets` -If multiple elements are placed into the same bucket, hash collisions may occur. To limit this, the container uses the **maximum load factor** as a threshold. When inserting an element would cause the load factor to exceed this value, the container automatically increases the number of buckets and performs a **rehash**. - -By default, the maximum load factor of an `unordered_set` is **1.0**. - +When the current load factor exceeds the maximum load factor, the container automatically rehashes to increase the number of buckets and maintain efficient lookup performance. ## Syntax ```pseudo -unordered_set_name.max_load_factor(); // returns the maximum load factor - -unordered_set_name.max_load_factor(double z); // sets z as the maximum load factor +unordered_set_name.max_load_factor(value); ``` +**Parameters:** + +- `value` (float, optional): The new maximum load factor to set for the container. + +**Return value:** + +- When called without arguments, returns the current maximum load factor as a `float`. +- When called with an argument, sets the maximum load factor and returns nothing (`void`). + ## Example This example demonstrates how to read and modify the maximum load factor and how it affects the number of buckets in the container. @@ -45,30 +44,24 @@ Most standard library implementations pre-allocate a relatively large number of #include int main() { - std::unordered_set s; - std::cout << "Initial maximum load factor: " << s.max_load_factor() << "\n"; - s.rehash(1); // Forces the container to start with 1 bucket - s.max_load_factor(0.5f); + std::unordered_set s; + std::cout << "Initial maximum load factor: " << s.max_load_factor() << "\n"; + s.rehash(1); // Forces the container to start with 1 bucket + s.max_load_factor(0.5f); - std::cout << "Initial buckets: " << s.bucket_count() << "\n\n"; + std::cout << "Initial buckets: " << s.bucket_count() << "\n\n"; - s.insert(5); + s.insert(5); - std::cout << "After inserting 5:\n"; - std::cout << "max_load_factor = " << s.max_load_factor() << "\n"; - std::cout << "size = " << s.size() << "\n"; - std::cout << "bucket_count = " << s.bucket_count() << "\n"; - std::cout << "load_factor = " << s.load_factor() << "\n"; + std::cout << "After inserting 5:\n"; + std::cout << "max_load_factor = " << s.max_load_factor() << "\n"; + std::cout << "size = " << s.size() << "\n"; + std::cout << "bucket_count = " << s.bucket_count() << "\n"; + std::cout << "load_factor = " << s.load_factor() << "\n"; } - ``` -Even though `rehash(1)` was called, the insertion caused the container to increase the number of buckets to satisfy the constraint: - -`size / bucket_count <= max_load_factor` - -Since `max_load_factor = 0.5`, at least two buckets are required for one element, and the implementation chose 23. -Output of this code: +The output of this code: ```shell Initial maximum load factor: 1 @@ -81,29 +74,35 @@ bucket_count = 23 load_factor = 0.0434783 ``` +> **Note:** Actual bucket counts may vary depending on the standard library implementation. + ## Codebyte Example -Test and modify the code below: +This example sets a custom maximum load factor before inserting elements, ensuring the container maintains lower collision density as it grows: ```codebyte/cpp #include #include int main() { - std::unordered_set s; - std::cout << "Initial maximum load factor: " << s.max_load_factor() << "\n"; - s.rehash(1); // Forces the container to start with 1 bucket - s.max_load_factor(0.5f); + std::unordered_set values; - std::cout << "Initial buckets: " << s.bucket_count() << "\n\n"; + // Set a stricter maximum load factor + values.max_load_factor(0.7f); - s.insert(5); + std::cout << "Max load factor set to: " + << values.max_load_factor() << "\n"; - std::cout << "After inserting 5:\n"; - std::cout << "max_load_factor = " << s.max_load_factor() << "\n"; - std::cout << "size = " << s.size() << "\n"; - std::cout << "bucket_count = " << s.bucket_count() << "\n"; - std::cout << "load_factor = " << s.load_factor() << "\n"; -} + // Insert multiple elements + for (int i = 0; i < 20; ++i) { + values.insert(i); + } + std::cout << "Size: " << values.size() << "\n"; + std::cout << "Bucket count: " << values.bucket_count() << "\n"; + std::cout << "Current load factor: " + << values.load_factor() << "\n"; + + return 0; +} ``` From d37a0242f30eb61ed058a815f34b2ff63991cc9c Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Tue, 13 Jan 2026 13:20:44 +0530 Subject: [PATCH 5/5] Update example for max load factor explanation This example shows how to read and set the maximum load factor and how it influences bucket allocation. `rehash()` is used to start with fewer buckets so the effect is easier to observe. --- .../unordered-set/terms/max-load-factor/max-load-factor.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/content/cpp/concepts/unordered-set/terms/max-load-factor/max-load-factor.md b/content/cpp/concepts/unordered-set/terms/max-load-factor/max-load-factor.md index 2c92421892f..53d02bde79b 100644 --- a/content/cpp/concepts/unordered-set/terms/max-load-factor/max-load-factor.md +++ b/content/cpp/concepts/unordered-set/terms/max-load-factor/max-load-factor.md @@ -36,8 +36,7 @@ unordered_set_name.max_load_factor(value); ## Example -This example demonstrates how to read and modify the maximum load factor and how it affects the number of buckets in the container. -Most standard library implementations pre-allocate a relatively large number of buckets, so `rehash()` is used here to force the container to start small and make the effect visible. +This example shows how to read and set the maximum load factor and how it influences bucket allocation. `rehash()` is used to start with fewer buckets so the effect is easier to observe: ```cpp #include