我有一个 unordered_set<shared_ptr<T>> us
我想知道是否有针k
在us
,但是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/