Share via


unordered_set::erase

Removes elements at specified positions.

iterator erase(iterator where);
iterator erase(iterator first, iterator last);
size_type erase(const Key& keyval);

Parameters

  • first
    Beginning of range to erase.

  • key
    Key value to erase.

  • last
    End of range to erase.

  • where
    Element to erase

Remarks

The first member function removes the element of the controlled sequence pointed to by where. The second member function removes the elements in the range [first, last). Both return an iterator that designates the first element remaining beyond any elements removed, or unordered_set::end() if no such element exists.

The third member removes the elements in the range delimited by unordered_set::equal_range(keyval). It returns the number of elements it removes.

The member functions never throw an exception.

Example

 

// std_tr1__unordered_set__unordered_set_erase.cpp 
// compile with: /EHsc 
#include <unordered_set> 
#include <iostream> 
 
typedef std::unordered_set<char> Myset; 
int main() 
    { 
    Myset c1; 
 
    c1.insert('a'); 
    c1.insert('b'); 
    c1.insert('c'); 
 
// display contents " [c] [b] [a]" 
    for (Myset::const_iterator it = c1.begin(); 
        it != c1.end(); ++it) 
        std::cout << " [" << *it << "]"; 
    std::cout << std::endl; 
 
// erase an element and reinspect 
    Myset::iterator it2 = c1.erase(c1.begin()); 
    std::cout << "*erase(begin()) == [" << *it2 << "]"; 
    std::cout << std::endl; 
 
// add elements and display " [e] [d] [b] [a]" 
    c1.insert('d'); 
    c1.insert('e'); 
 
    for (Myset::const_iterator it = c1.begin(); 
        it != c1.end(); ++it) 
        std::cout << " [" << *it << "]"; 
    std::cout << std::endl; 
 
// erase all but end; 
    it2 = c1.end(); 
    it2 = c1.erase(c1.begin(), --it2); 
    std::cout << "*erase(begin(), end()-1) == [" 
        << *it2 << "]" << std::endl; 
    std::cout << "size() == " << c1.size() << std::endl; 
 
    return (0); 
    } 
 
 [c] [b] [a]
*erase(begin()) == [b]
 [e] [d] [b] [a]
*erase(begin(), end()-1) == [a]
size() == 1

Requirements

Header: <unordered_set>

Namespace: std

See Also

Reference

<unordered_set>

unordered_set Class

unordered_set::clear

Other Resources

<unordered_set> Members