为什么std::unordered_set()
中有非常量的find()
?
iterator find( const Key& key );
const_iterator find( const Key& key ) const;
iterator
与 const_iterator
相同,为什么有非常量版本的 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()?
因为iterator不像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()
返回的特定类型的迭代器,并且它应该与其他容器一致。
关于c++ - std::unordered_set 中的非常量 find(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47658296/