From 3e6b450a55d4eabd07fa2e4ae37db3ce0810d3fe Mon Sep 17 00:00:00 2001 From: Rishabh Ranjan Singh Date: Sat, 13 Dec 2025 12:14:53 +0530 Subject: [PATCH 1/2] docs: Add C++ unordered_set::clear() term entry (#8030) --- .../unordered-set/terms/clear/clear.md | 110 ++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 content/cpp/concepts/unordered-set/terms/clear/clear.md diff --git a/content/cpp/concepts/unordered-set/terms/clear/clear.md b/content/cpp/concepts/unordered-set/terms/clear/clear.md new file mode 100644 index 00000000000..04742816ed7 --- /dev/null +++ b/content/cpp/concepts/unordered-set/terms/clear/clear.md @@ -0,0 +1,110 @@ +--- +Title: '.clear()' +Description: 'Removes all elements from the unordered_set, leaving the container with a size of zero.' +Subjects: + - 'Computer Science' + - 'Programming' +Tags: + - 'C++' + - 'Unordered Set' + - 'STL' + - 'Containers' +CatalogContent: + - 'learn-c-plus-plus' + - 'paths/computer-science' +--- + +The **`.clear()`** method is used to remove all elements from an `std::unordered_set`. After calling this method, the set will be empty, and its size will be zero. + +The capacity of the set's internal storage (the number of buckets) is typically **not** reduced by `clear()`. The allocated memory for the container's structure often remains intact, although the memory occupied by the individual elements is deallocated. + +All iterators, pointers, and references pointing to elements within the set are invalidated after calling `clear()`. + +## Syntax + +The `.clear()` method is called directly on the `unordered_set` object and takes no arguments. + +```cpp +unordered_set_name.clear(); +``` + +## Parameters + +The method takes no parameters. + +## Return Value + +The method returns `void` (nothing). + +## Example + +This example demonstrates using `.clear()` to empty a set and confirms the change by checking the size before and after the operation. + +```cpp +#include +#include +#include + +int main() { + std::unordered_set planets = { + "Mercury", + "Venus", + "Earth", + "Mars" + }; + + std::cout << "--- Before clear() ---\n"; + std::cout << "Size: " << planets.size() << "\n"; + + // Call clear() to remove all elements + planets.clear(); + + std::cout << "\n--- After clear() ---\n"; + std::cout << "Size: " << planets.size() << "\n"; + + if (planets.empty()) { + std::cout << "The set is now empty.\n"; + } + + return 0; +} +``` + +Output: + +``` +--- Before clear() --- +Size: 4 + +--- After clear() --- +Size: 0 +The set is now empty. +``` + +## Codebyte + +Use the Codebyte below to practice using `.clear()` on a set of integers. + +```cpp +#include +#include + +int main() { + std::unordered_set data_points = {10, 20, 30, 40, 50}; + + // Print initial size + std::cout << "Initial size: " << data_points.size() << "\n"; + + // Clear the set + data_points.clear(); + + // Print final size + std::cout << "Final size: " << data_points.size() << "\n"; + + // Attempting to insert a new element (the set is still valid) + data_points.insert(999); + std::cout << "Size after new insert: " << data_points.size() << "\n"; + + return 0; +} +``` \ No newline at end of file From 54d95a71e3326fee6e1cc8f8ab2d4bab24dbcb06 Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Mon, 15 Dec 2025 16:44:20 +0530 Subject: [PATCH 2/2] [Term Entry] C++ Unordered-sets: clear() Updated the documentation for the clear() method in unordered_set. Adjusted title, description, and example formatting. --- .../unordered-set/terms/clear/clear.md | 47 +++++++++---------- 1 file changed, 22 insertions(+), 25 deletions(-) diff --git a/content/cpp/concepts/unordered-set/terms/clear/clear.md b/content/cpp/concepts/unordered-set/terms/clear/clear.md index 04742816ed7..83a36800d88 100644 --- a/content/cpp/concepts/unordered-set/terms/clear/clear.md +++ b/content/cpp/concepts/unordered-set/terms/clear/clear.md @@ -1,44 +1,41 @@ --- -Title: '.clear()' -Description: 'Removes all elements from the unordered_set, leaving the container with a size of zero.' +Title: 'clear()' +Description: 'Removes all elements from the unordered_set, leaving the container empty.' Subjects: - 'Computer Science' - - 'Programming' + - 'Game Development' Tags: - - 'C++' - - 'Unordered Set' - - 'STL' - 'Containers' + - 'Sets' + - 'STL' CatalogContent: - 'learn-c-plus-plus' - 'paths/computer-science' --- -The **`.clear()`** method is used to remove all elements from an `std::unordered_set`. After calling this method, the set will be empty, and its size will be zero. +The **`clear()`** method is used to remove all elements from an `std::unordered_set`. After calling this method, the set will be empty, and its size will be zero. -The capacity of the set's internal storage (the number of buckets) is typically **not** reduced by `clear()`. The allocated memory for the container's structure often remains intact, although the memory occupied by the individual elements is deallocated. +The capacity of the set's internal storage (the number of buckets) is typically not reduced by `clear()`. The container’s bucket structure is typically preserved, while memory used by individual elements is released. All iterators, pointers, and references pointing to elements within the set are invalidated after calling `clear()`. ## Syntax -The `.clear()` method is called directly on the `unordered_set` object and takes no arguments. - -```cpp +```pseudo unordered_set_name.clear(); ``` -## Parameters +**Parameters:** The method takes no parameters. -## Return Value +**Return value:** The method returns `void` (nothing). ## Example -This example demonstrates using `.clear()` to empty a set and confirms the change by checking the size before and after the operation. +In this example, `clear()` is used to remove all elements from an `unordered_set` and verify that its size becomes zero: ```cpp #include @@ -55,13 +52,13 @@ int main() { std::cout << "--- Before clear() ---\n"; std::cout << "Size: " << planets.size() << "\n"; - + // Call clear() to remove all elements planets.clear(); std::cout << "\n--- After clear() ---\n"; std::cout << "Size: " << planets.size() << "\n"; - + if (planets.empty()) { std::cout << "The set is now empty.\n"; } @@ -70,9 +67,9 @@ int main() { } ``` -Output: +The output of this code is: -``` +```shell --- Before clear() --- Size: 4 @@ -81,23 +78,23 @@ Size: 0 The set is now empty. ``` -## Codebyte +## Codebyte Example -Use the Codebyte below to practice using `.clear()` on a set of integers. +In this example, `clear()` removes all elements from an `unordered_set`, after which new elements can still be inserted: -```cpp +```codebyte/cpp #include #include int main() { std::unordered_set data_points = {10, 20, 30, 40, 50}; - + // Print initial size std::cout << "Initial size: " << data_points.size() << "\n"; - + // Clear the set data_points.clear(); - + // Print final size std::cout << "Final size: " << data_points.size() << "\n"; @@ -107,4 +104,4 @@ int main() { return 0; } -``` \ No newline at end of file +```