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

bit*_*ask 8 c++ const unordered-set

我有一个unordered_set<shared_ptr<T>> us,我想知道针k是否在us,但k有类型shared_ptr<T const>所以unordered_set<shared_ptr<T>>::find抱怨它无法转换.

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

我确实尝试过const_cast(感觉很脏),但没有削减它.

Dei*_*Dei 10

使用std::const_pointer_cast是一种可能的解决方案.

us.find(std::const_pointer_cast<T>(k));
Run Code Online (Sandbox Code Playgroud)

因为你没有修改k,所以抛弃const是可以的.