std::unordered_set 中的非常量 find()

non-const find() in std::unordered_set

为什么 std::unordered_set() 中有非常量 find()

iterator find( const Key& key );
const_iterator find( const Key& key ) const;

iteratorconst_iterator一样,为什么会有非const版本的find()

http://en.cppreference.com/w/cpp/container/unordered_set/find

iterator is the same as const_iterator, why there is non-const version of find()?

因为迭代器不像const_iterator一样是强制性的,如documentation:

所述

The member types iterator and const_iterator may be aliases to the same type. Since iterator is convertible to const_iterator, const_iterator should be used in function parameter lists to avoid violations of the One Definition Rule.

重点是我的。由于它们不是强制性的,因此一些通用代码可以依赖于 find() 返回的特定类型的迭代器,并且它应该与其他容器一致。