You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.'
4
4
Subjects:
5
5
- 'Computer Science'
6
-
- 'Programming'
6
+
- 'Game Development'
7
7
Tags:
8
-
- 'C++'
9
-
- 'Unordered Set'
10
-
- 'STL'
11
8
- 'Containers'
9
+
- 'Sets'
10
+
- 'STL'
12
11
CatalogContent:
13
12
- 'learn-c-plus-plus'
14
13
- 'paths/computer-science'
15
14
---
16
15
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.
18
17
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.
20
19
21
20
All iterators, pointers, and references pointing to elements within the set are invalidated after calling `clear()`.
22
21
23
22
## Syntax
24
23
25
-
The `.clear()` method is called directly on the `unordered_set` object and takes no arguments.
26
-
27
-
```cpp
24
+
```pseudo
28
25
unordered_set_name.clear();
29
26
```
30
27
31
-
## Parameters
28
+
**Parameters:**
32
29
33
30
The method takes no parameters.
34
31
35
-
## Return Value
32
+
**Return value:**
36
33
37
34
The method returns `void` (nothing).
38
35
39
36
## Example
40
37
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:
42
39
43
40
```cpp
44
41
#include<iostream>
@@ -55,13 +52,13 @@ int main() {
55
52
56
53
std::cout << "--- Before clear() ---\n";
57
54
std::cout << "Size: " << planets.size() << "\n";
58
-
55
+
59
56
// Call clear() to remove all elements
60
57
planets.clear();
61
58
62
59
std::cout << "\n--- After clear() ---\n";
63
60
std::cout << "Size: " << planets.size() << "\n";
64
-
61
+
65
62
if (planets.empty()) {
66
63
std::cout << "The set is now empty.\n";
67
64
}
@@ -70,9 +67,9 @@ int main() {
70
67
}
71
68
```
72
69
73
-
Output:
70
+
The output of this code is:
74
71
75
-
```
72
+
```shell
76
73
--- Before clear() ---
77
74
Size: 4
78
75
@@ -81,23 +78,23 @@ Size: 0
81
78
The set is now empty.
82
79
```
83
80
84
-
## Codebyte
81
+
## Codebyte Example
85
82
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:
0 commit comments