c++ - 在只有 const shared_ptr 的 unordered_set 中找到一个 shared_ptr?

标签 c++ constants unordered-set

我有一个 unordered_set<shared_ptr<T>> us我想知道是否有针kus ,但是k类型为 shared_ptr<T const>所以unordered_set<shared_ptr<T>>::find提示它无法转换。

有解决办法吗?也许通过直接提供哈希?

我试过const_cast (感觉很脏)但这并没有解决问题。

最佳答案

使用 std::const_pointer_cast在这里是一个可能的解决方案。

us.find(std::const_pointer_cast<T>(k));

因为您没有修改 k,所以可以丢弃 const。

关于c++ - 在只有 const shared_ptr 的 unordered_set 中找到一个 shared_ptr?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50758406/

相关文章:

c++ - C++中boost vector 的子类

c++ - 在 C++ 中将函数指针中的 const 参数转换为非 const

c++ - 我应该制作所有不应该改变常量的东西吗?

c++ - 了解 C++ 中对迭代器的常量引用和非常量引用。为什么我不能在打印函数中使用对迭代器的非常量引用?

c++ - std::为无序集(或映射)插入迭代器?

c++ - 是否可以用硬件平方根执行除法运算?

C++ 从 BST 中删除一个节点

c++ - 查找#define值dll

c++ - 如何在 C++ 中创建一组无序的整数对?

C++ bool std::operator < 带有键 std::unordered_set<int,std::hash<int>> 的映射错误