std::unordered_set<Key,Hash,KeyEqual,Allocator>::erase
| (1) | ||
| iterator erase( const_iterator pos ); |
(since C++11) | |
| iterator erase( iterator pos ); |
(since C++17) | |
| iterator erase( const_iterator first, const_iterator last ); |
(2) | (since C++11) |
| size_type erase( const key_type& key ); |
(3) | (since C++11) |
Removes specified elements from the container.
pos.[first; last), which must be a valid range in *this.key.References and iterators to the erased elements are invalidated. Other iterators and references are not invalidated.
The iterator pos must be valid and dereferenceable. Thus the end() iterator (which is valid, but is not dereferenceable) cannot be used as a value for pos.
The order of the elements that are not erased is preserved. (This makes it possible to erase individual elements while iterating through the container.)
Contents |
[edit] Parameters
| pos | - | iterator to the element to remove |
| first, last | - | range of elements to remove |
| key | - | key value of the elements to remove |
[edit] Return value
0 or 1).[edit] Exceptions
Compare object.[edit] Complexity
Given an instance c of unordered_set:
[edit] Example
#include <unordered_set> #include <iostream> int main() { std::unordered_set<int> c = {1, 2, 3, 4, 5, 6, 7, 8, 9}; // erase all odd numbers from c for(auto it = c.begin(); it != c.end(); ) { if(*it % 2 != 0) it = c.erase(it); else ++it; } for(int n : c) { std::cout << n << ' '; } }
Possible output:
2 4 6 8
Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
| DR | Applied to | Behavior as published | Correct behavior |
|---|---|---|---|
| LWG 2356 | C++11 | the order of element that are not erased was unspecified | required to be preserved |
[edit] See also
| (C++11) |
clears the contents (public member function) |

Formed in 2009, the Archive Team (not to be confused with the archive.org Archive-It Team) is a rogue archivist collective dedicated to saving copies of rapidly dying or deleted websites for the sake of history and digital heritage. The group is 100% composed of volunteers and interested parties, and has expanded into a large amount of related projects for saving online and digital history.
