Skip to content

Commit 54d95a7

Browse files
[Term Entry] C++ Unordered-sets: clear()
Updated the documentation for the clear() method in unordered_set. Adjusted title, description, and example formatting.
1 parent 3e6b450 commit 54d95a7

File tree

1 file changed

+22
-25
lines changed
  • content/cpp/concepts/unordered-set/terms/clear

1 file changed

+22
-25
lines changed
Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,41 @@
11
---
2-
Title: '.clear()'
3-
Description: 'Removes all elements from the unordered_set, leaving the container with a size of zero.'
2+
Title: 'clear()'
3+
Description: 'Removes all elements from the unordered_set, leaving the container empty.'
44
Subjects:
55
- 'Computer Science'
6-
- 'Programming'
6+
- 'Game Development'
77
Tags:
8-
- 'C++'
9-
- 'Unordered Set'
10-
- 'STL'
118
- 'Containers'
9+
- 'Sets'
10+
- 'STL'
1211
CatalogContent:
1312
- 'learn-c-plus-plus'
1413
- 'paths/computer-science'
1514
---
1615

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.
16+
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.
1817

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.
18+
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.
2019

2120
All iterators, pointers, and references pointing to elements within the set are invalidated after calling `clear()`.
2221

2322
## Syntax
2423

25-
The `.clear()` method is called directly on the `unordered_set` object and takes no arguments.
26-
27-
```cpp
24+
```pseudo
2825
unordered_set_name.clear();
2926
```
3027

31-
## Parameters
28+
**Parameters:**
3229

3330
The method takes no parameters.
3431

35-
## Return Value
32+
**Return value:**
3633

3734
The method returns `void` (nothing).
3835

3936
## Example
4037

41-
This example demonstrates using `.clear()` to empty a set and confirms the change by checking the size before and after the operation.
38+
In this example, `clear()` is used to remove all elements from an `unordered_set` and verify that its size becomes zero:
4239

4340
```cpp
4441
#include <iostream>
@@ -55,13 +52,13 @@ int main() {
5552

5653
std::cout << "--- Before clear() ---\n";
5754
std::cout << "Size: " << planets.size() << "\n";
58-
55+
5956
// Call clear() to remove all elements
6057
planets.clear();
6158

6259
std::cout << "\n--- After clear() ---\n";
6360
std::cout << "Size: " << planets.size() << "\n";
64-
61+
6562
if (planets.empty()) {
6663
std::cout << "The set is now empty.\n";
6764
}
@@ -70,9 +67,9 @@ int main() {
7067
}
7168
```
7269

73-
Output:
70+
The output of this code is:
7471

75-
```
72+
```shell
7673
--- Before clear() ---
7774
Size: 4
7875

@@ -81,23 +78,23 @@ Size: 0
8178
The set is now empty.
8279
```
8380

84-
## Codebyte
81+
## Codebyte Example
8582

86-
Use the Codebyte below to practice using `.clear()` on a set of integers.
83+
In this example, `clear()` removes all elements from an `unordered_set`, after which new elements can still be inserted:
8784

88-
```cpp
85+
```codebyte/cpp
8986
#include <iostream>
9087
#include <unordered_set>
9188
9289
int main() {
9390
std::unordered_set<int> data_points = {10, 20, 30, 40, 50};
94-
91+
9592
// Print initial size
9693
std::cout << "Initial size: " << data_points.size() << "\n";
97-
94+
9895
// Clear the set
9996
data_points.clear();
100-
97+
10198
// Print final size
10299
std::cout << "Final size: " << data_points.size() << "\n";
103100
@@ -107,4 +104,4 @@ int main() {
107104
108105
return 0;
109106
}
110-
```
107+
```

0 commit comments

Comments
 (0)