|
| 1 | +--- |
| 2 | +Title: '.clear()' |
| 3 | +Description: 'Removes all elements from the unordered_set, leaving the container with a size of zero.' |
| 4 | +Subjects: |
| 5 | + - 'Computer Science' |
| 6 | + - 'Programming' |
| 7 | +Tags: |
| 8 | + - 'C++' |
| 9 | + - 'Unordered Set' |
| 10 | + - 'STL' |
| 11 | + - 'Containers' |
| 12 | +CatalogContent: |
| 13 | + - 'learn-c-plus-plus' |
| 14 | + - 'paths/computer-science' |
| 15 | +--- |
| 16 | + |
| 17 | +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. |
| 18 | + |
| 19 | +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. |
| 20 | + |
| 21 | +All iterators, pointers, and references pointing to elements within the set are invalidated after calling `clear()`. |
| 22 | + |
| 23 | +## Syntax |
| 24 | + |
| 25 | +The `.clear()` method is called directly on the `unordered_set` object and takes no arguments. |
| 26 | + |
| 27 | +```cpp |
| 28 | +unordered_set_name.clear(); |
| 29 | +``` |
| 30 | + |
| 31 | +## Parameters |
| 32 | + |
| 33 | +The method takes no parameters. |
| 34 | + |
| 35 | +## Return Value |
| 36 | + |
| 37 | +The method returns `void` (nothing). |
| 38 | + |
| 39 | +## Example |
| 40 | + |
| 41 | +This example demonstrates using `.clear()` to empty a set and confirms the change by checking the size before and after the operation. |
| 42 | + |
| 43 | +```cpp |
| 44 | +#include <iostream> |
| 45 | +#include <string> |
| 46 | +#include <unordered_set> |
| 47 | + |
| 48 | +int main() { |
| 49 | + std::unordered_set<std::string> planets = { |
| 50 | + "Mercury", |
| 51 | + "Venus", |
| 52 | + "Earth", |
| 53 | + "Mars" |
| 54 | + }; |
| 55 | + |
| 56 | + std::cout << "--- Before clear() ---\n"; |
| 57 | + std::cout << "Size: " << planets.size() << "\n"; |
| 58 | + |
| 59 | + // Call clear() to remove all elements |
| 60 | + planets.clear(); |
| 61 | + |
| 62 | + std::cout << "\n--- After clear() ---\n"; |
| 63 | + std::cout << "Size: " << planets.size() << "\n"; |
| 64 | + |
| 65 | + if (planets.empty()) { |
| 66 | + std::cout << "The set is now empty.\n"; |
| 67 | + } |
| 68 | + |
| 69 | + return 0; |
| 70 | +} |
| 71 | +``` |
| 72 | + |
| 73 | +Output: |
| 74 | + |
| 75 | +``` |
| 76 | +--- Before clear() --- |
| 77 | +Size: 4 |
| 78 | +
|
| 79 | +--- After clear() --- |
| 80 | +Size: 0 |
| 81 | +The set is now empty. |
| 82 | +``` |
| 83 | + |
| 84 | +## Codebyte |
| 85 | + |
| 86 | +Use the Codebyte below to practice using `.clear()` on a set of integers. |
| 87 | + |
| 88 | +```cpp |
| 89 | +#include <iostream> |
| 90 | +#include <unordered_set> |
| 91 | + |
| 92 | +int main() { |
| 93 | + std::unordered_set<int> data_points = {10, 20, 30, 40, 50}; |
| 94 | + |
| 95 | + // Print initial size |
| 96 | + std::cout << "Initial size: " << data_points.size() << "\n"; |
| 97 | + |
| 98 | + // Clear the set |
| 99 | + data_points.clear(); |
| 100 | + |
| 101 | + // Print final size |
| 102 | + std::cout << "Final size: " << data_points.size() << "\n"; |
| 103 | + |
| 104 | + // Attempting to insert a new element (the set is still valid) |
| 105 | + data_points.insert(999); |
| 106 | + std::cout << "Size after new insert: " << data_points.size() << "\n"; |
| 107 | + |
| 108 | + return 0; |
| 109 | +} |
| 110 | +``` |
0 commit comments