c++ - What happens when we try to erase non existent key from hash_set -
what happens when try erase non existent key hash_set
class sgi's stl? call hash_set::erase
first try find key , delete it?
here code used implementation of hash_set
, it's erase method of hashtable
:
template <class _val, class _key, class _hf, class _ex, class _eq, class _all> typename hashtable<_val,_key,_hf,_ex,_eq,_all>::size_type hashtable<_val,_key,_hf,_ex,_eq,_all>::erase(const key_type& __key) { const size_type __n = _m_bkt_num_key(__key); _node* __first = _m_buckets[__n]; size_type __erased = 0; if (__first) { _node* __cur = __first; _node* __next = __cur->_m_next; while (__next) { if (_m_equals(_m_get_key(__next->_m_val), __key)) { __cur->_m_next = __next->_m_next; _m_delete_node(__next); __next = __cur->_m_next; ++__erased; --_m_num_elements; } else { __cur = __next; __next = __cur->_m_next; } } if (_m_equals(_m_get_key(__first->_m_val), __key)) { _m_buckets[__n] = __first->_m_next; _m_delete_node(__first); ++__erased; --_m_num_elements; } } return __erased; }
as can see, tries find key before deleting node, , does nothing if key doesn't exist.
also, sgi documentation :
erase key : destroys elements key same k, , removes them a. return value number of elements erased, i.e. old value of a.count(k).
Comments
Post a Comment