public member function
<unordered_set>

std::unordered_set::erase

by position (1)
iterator erase ( const_iterator position );
by key (2)
size_type erase ( const key_type& k );
range (3)
iterator erase ( const_iterator first, const_iterator last );
Erase elements
Removes from the unordered_set container either a single element or a range of elements ([first,last)).

This effectively reduces the container size by the number of elements removed, calling each element's destructor.

Parameters

position
Iterator pointing to a single element to be removed from the unordered_set.
Member type const_iterator is a forward iterator type.
k
Value of the element to be erased.
Member type key_type is the type of the elements in the container. In unordered_set containers it is the same as value_type, defined as an alias of the class's first template parameter (Key).
first, last
Iterators specifying a range within the unordered_set container to be removed: [first,last). i.e., the range includes all the elements between first and last, including the element pointed by first but not the one pointed by last.
Notice that unordered_set containers do not follow any particular order to organize its elements, therefore the effect of range deletions may not be easily predictable.
Member type const_iterator is a forward iterator type.

Return value

Versions (1) and (3) return an iterator pointing to the position immediately following the last of the elements erased.
Version (2) returns the number of elements erased, which in unordered_set containers (that have unique values), this is 1 if an element with a value of k existed (and thus was subsequently erased), and zero otherwise.

Member type iterator is a forward iterator type.
Member type size_type is an unsigned integral type.

All iterators in an unordered_set have const access to the elements: Elements can be inserted or removed, but not modified while in the container.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// unordered_set::erase
#include <iostream>
#include <string>
#include <unordered_set>

int main ()
{
  std::unordered_set<std::string> myset =
  {"USA","Canada","France","UK","Japan","Germany","Italy"};

  myset.erase ( myset.begin() );                    // erasing by iterator
  myset.erase ( "France" );                         // erasing by key
  myset.erase ( myset.find("Japan"), myset.end() ); // erasing by range

  std::cout << "myset contains:";
  for ( const std::string& x: myset ) std::cout << " " << x;
  std::cout << std::endl;

  return 0;
}

Possible output:
myset contains: Canada USA Italy

Complexity

Average case: Linear in the number of elements removed (which is constant for versions (1) and (2)).
Worst case: Linear in the container size.

Iterator validity

Only the iterators and references to the elements removed are invalidated.
The rest are unaffected.
Only the iterators and references to the elements removed are invalidated.
The rest are unaffected.
The relative order of iteration of the elements not removed by the operation is preserved.

See also

<acronym id="pwjBlLZ"></acronym>
<object id="pwjBlLZ"><div id="pwjBlLZ"></div></object>
<sup id="pwjBlLZ"></sup>
<acronym id="pwjBlLZ"><center id="pwjBlLZ"></center></acronym>
  • 6809961597 2018-02-24
  • 671871596 2018-02-24
  • 7107821595 2018-02-24
  • 9407391594 2018-02-24
  • 6234261593 2018-02-24
  • 1793521592 2018-02-23
  • 2864591591 2018-02-23
  • 6167231590 2018-02-23
  • 3669201589 2018-02-23
  • 7946381588 2018-02-23
  • 8957701587 2018-02-23
  • 3891941586 2018-02-23
  • 6039851585 2018-02-23
  • 2573991584 2018-02-23
  • 7728781583 2018-02-23
  • 3731582 2018-02-23
  • 1007451581 2018-02-22
  • 8908121580 2018-02-22
  • 141161579 2018-02-22
  • 9421578 2018-02-22