unordered_map::erase
Visual Studio 2012
Removes elements at specified positions.
iterator erase(iterator where); iterator erase(iterator first, iterator last); size_type erase(const Key& keyval);
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_map::end() if no such element exists.
The third member removes the elements in the range delimited by unordered_map::equal_range(keyval). It returns the number of elements it removes.
The member functions never throw an exception.
// std_tr1__unordered_map__unordered_map_erase.cpp
// compile with: /EHsc
#include <unordered_map>
#include <iostream>
typedef std::unordered_map<char, int> Mymap;
int main()
{
Mymap c1;
c1.insert(Mymap::value_type('a', 1));
c1.insert(Mymap::value_type('b', 2));
c1.insert(Mymap::value_type('c', 3));
// display contents " [c 3] [b 2] [a 1]"
for (Mymap::const_iterator it = c1.begin();
it != c1.end(); ++it)
std::cout << " [" << it->first << ", " << it->second << "]";
std::cout << std::endl;
// erase an element and reinspect
Mymap::iterator it2 = c1.erase(c1.begin());
std::cout << "*erase(begin()) == ["
<< it2->first << ", " << it2->second << "]";
std::cout << std::endl;
// add elements and display " [e 5] [d 4] [b 2] [a 1]"
c1.insert(Mymap::value_type('d', 4));
c1.insert(Mymap::value_type('e', 5));
for (Mymap::const_iterator it = c1.begin();
it != c1.end(); ++it)
std::cout << " [" << it->first << ", " << it->second << "]";
std::cout << std::endl;
// erase all but end;
it2 = c1.end();
it2 = c1.erase(c1.begin(), --it2);
std::cout << "*erase(begin(), end()-1) == ["
<< it2->first << ", " << it2->second << "]" << std::endl;
std::cout << "size() == " << c1.size() << std::endl;
return (0);
}
[c, 3] [b, 2] [a, 1] *erase(begin()) == [b, 2] [e, 5] [d, 4] [b, 2] [a, 1] *erase(begin(), end()-1) == [a, 1] size() == 1