c++ - std::unordered_set 中的非常量 find()

标签 c++ c++11 unordered-set

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

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

iteratorconst_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/

相关文章:

c++ - Windows .NET (C++) SpinWait 在 Linux 和 Mac OS X 上的等价物是什么?

c++ - Win32 C++ : Creation and Calling method from separate C++ file

c++ - unordered_set如何确定c++中的插入顺序?

c++ - 如何遍历 C++ 中的无序集?

c++ - unordered_set 与 boost 和 standard 的区别

c++ - 按位运算比调用memcpy更快吗?

c++ - SFML 未静态链接到 openal32(静态链接到所有其他依赖项)

c++ - 根据参数的值类别调用函数

C++:为什么这个 constexpr 不是编译时常量

c++ - 是否有类型特征可以立即删除顶级 cv 和引用?