C++:在指针集中查找

C++: find in set of pointers

本文关键字:集中 查找 指针 C++      更新时间:2023-10-16

下面的例子说明了我的问题:

#include <set>
class A {};
int main()
{
    A a;
    A * p = &a;
    const A * cp = &a;
    std::set<A*> s;
    s.insert(p);
    s.find(cp);
}

编译结束于:

a.cpp: In function ‘int main()’:
a.cpp:13:18: error: invalid conversion from ‘const A*’ to ‘std::set<A*>::key_type {aka A*}’ [-fpermissive]
         s.find(cp);
                  ^
In file included from /usr/include/c++/4.9.1/set:61:0,
                 from a.cpp:1:
/usr/include/c++/4.9.1/bits/stl_set.h:701:7: note: initializing argument 1 of ‘std::set<_Key, _Compare, _Alloc>::iterator std::set<_Key, _Compare, _Alloc>::find(const key_type&) [with _Key = A*; _Compare = std::less<A*>; _Alloc = std::allocator<A*>; std::set<_Key, _Compare, _Alloc>::iterator = std::_Rb_tree_const_iterator<A*>; std::set<_Key, _Compare, _Alloc>::key_type = A*]’
       find(const key_type& __x)

我知道它为什么不编译,但有没有比s.find((A*)cp)更丑陋、更残酷的解决方案?同时给出了set指针和const指针。

一个选项是将C++14透明运算符函子与异构查找一起使用:

std::set<A*, std::less<>> s;
s.find(cp);

不幸的是,libstdc++目前不支持异构查找,但它被标记为WIP。(它在clang/libc++中提供,并将在Visual Studio的下一个版本中提供。)如果没有它,你几乎只能使用const_castcp

不幸的是,set的查询方法没有在键类型上模板化(它们确实应该是),所以lower_boundequal_range等没有帮助。

假设变量类型是不可协商的,那么您唯一的选择就是去掉指针的constness,或者reinterpret_cast,将其设置为set<const A*>。后者在某些方面让我感觉更好,但从技术上讲是不安全的。

抛开惊愕。使用const_cast来做这件事,以明确这是演员阵容正在做的唯一事情。如果你喜欢的话,把它和find包装在一个免费函数中;如果一段代码是邪恶的,那么最好让邪恶的东西成为它所做的唯一的东西。

您可以将find与const_cast一起使用:

s.find(const_cast<A*>(cp));