|
| 1 | +--- |
| 2 | +Title: 'erase()' |
| 3 | +Description: 'removes elements from an unordered set' |
| 4 | +Subjects: |
| 5 | + - 'Computer Science' |
| 6 | +Tags: |
| 7 | + - 'Methods' |
| 8 | + - 'unordered-sets' |
| 9 | +CatalogContent: |
| 10 | + - 'learn-C++' |
| 11 | + - 'paths/computer-science' |
| 12 | +--- |
| 13 | + |
| 14 | +# 'C++ Unordered-sets: erase()' |
| 15 | + |
| 16 | +The 'erase()' function removes elements from an unordered set. |
| 17 | +It can erase a single element by key, a single element by iterator, or a range of elements using two iterators. |
| 18 | +Only iterators pointing to erased elements are invalidated; all others remain valid. |
| 19 | + |
| 20 | +## Syntax |
| 21 | + |
| 22 | +Erase by Key |
| 23 | +``` |
| 24 | +size_t erase(const Key& key); |
| 25 | +``` |
| 26 | + |
| 27 | +Erase by Iterator |
| 28 | +``` |
| 29 | +iterator erase(const_iterator pos); |
| 30 | +``` |
| 31 | + |
| 32 | +Erase a Range of Iterators |
| 33 | +``` |
| 34 | +iterator erase(const_iterator first, const_iterator last); |
| 35 | +``` |
| 36 | + |
| 37 | +Erase by Key: Removes all elements matching `key` (in an `unordered_set`, that's at most one). Returns the number of elements removed. |
| 38 | + |
| 39 | +Erase by Iterator: Removes the element at `pos`. Returns an iterator to the element that followed the erased one. |
| 40 | + |
| 41 | +Erase a Range of Iterators: Removes all elements in the half-open range `[first, last]`. Returns an iterator to the element that followed the last removed one. |
| 42 | + |
| 43 | +## Example |
| 44 | + |
| 45 | +```cpp |
| 46 | +#include <iostream> |
| 47 | +#include <unordered_set> |
| 48 | + |
| 49 | +int main() { |
| 50 | + std::unordered_set<int> numbers = {1, 2, 3, 4, 5}; |
| 51 | + |
| 52 | + // Erase by key |
| 53 | + numbers.erase(3); |
| 54 | + |
| 55 | + // Erase by iterator |
| 56 | + auto it = numbers.find(4); |
| 57 | + if (it != numbers.end()) { |
| 58 | + numbers.erase(it); |
| 59 | + } |
| 60 | + |
| 61 | + // Erase using a range |
| 62 | + auto first = numbers.begin(); |
| 63 | + auto last = numbers.find(5); |
| 64 | + numbers.erase(first, last); |
| 65 | + |
| 66 | + for (int n : numbers) { |
| 67 | + std::cout << n << " "; // expected output: 5 |
| 68 | + } |
| 69 | +} |
| 70 | +``` |
| 71 | +## Codebyte |
| 72 | + |
| 73 | +The following example creates an `unordered_set<int>`, then demonstrates all three `erase()` overloads: |
| 74 | +* Erasing by key |
| 75 | +* Erasing by iterators |
| 76 | +* Erasing by iterator range |
| 77 | + |
| 78 | +```codebyte/cpp |
| 79 | +#include <iostream> |
| 80 | +#include <unordered_set> |
| 81 | +
|
| 82 | +int main() { |
| 83 | + std::unordered_set<int> s = {1, 2, 3, 4, 5}; |
| 84 | +
|
| 85 | + // 1. Erase by key |
| 86 | + s.erase(3); |
| 87 | + // set is now {1, 2, 4, 5} |
| 88 | +
|
| 89 | + // 2. Erase by iterator |
| 90 | + auto it = s.find(4); |
| 91 | + if (it != s.end()) { |
| 92 | + s.erase(it); |
| 93 | + } |
| 94 | + // set is now {1, 2, 5} |
| 95 | +
|
| 96 | + // 3. Erase by iterator range |
| 97 | + auto first = s.begin(); |
| 98 | + auto last = s.end(); |
| 99 | + // this removes everything in the set |
| 100 | + s.erase(first, last); |
| 101 | +
|
| 102 | + // set is now empty |
| 103 | +
|
| 104 | + return 0; |
| 105 | +} |
| 106 | +``` |
0 commit comments