如何使用自定义比较器声明/使用三元组 (
unordered_set
) tuple
?
我需要将
float
(处理为 tuple
)的三元组存储在一个集合中以检查是否存在重复项。因为它是关于 float
,我想使用常规比较与 ==
是行不通的,所以需要自定义比较。
这个最小的代码无法编译:
>> cat unordered_set_triplet.cpp
#include <unordered_set>
#include <tuple>
#include <limits> // numeric_limits
#include <cmath> // fabs
#include <functional> // hash
using triplet = std::tuple<float, float, float>;
bool triplet_equal(triplet const & lhs, triplet const & rhs) {
float const eps = std::numeric_limits<float>::epsilon();
if (std::fabs(std::get<0>(lhs) - std::get<0>(rhs)) > eps) return false;
if (std::fabs(std::get<1>(lhs) - std::get<1>(rhs)) > eps) return false;
if (std::fabs(std::get<2>(lhs) - std::get<2>(rhs)) > eps) return false;
return true;
}
using unordered_set_triplet = std::unordered_set<triplet,
std::hash<triplet>,
decltype(triplet_equal)>;
int main() {
//unordered_set_triplet s; // Compilation: KO...
unordered_set_triplet s(10, std::hash<triplet>, triplet_equal);
s.insert({1.f, 2.f, 3.f});
}
我得到:
>> g++ -std=c++20 -o unordered_set_triplet unordered_set_triplet.cpp
In file included from /usr/include/c++/12/bits/hashtable.h:35,
from /usr/include/c++/12/unordered_set:46,
from unordered_set_triplet.cpp:1:
/usr/include/c++/12/bits/hashtable_policy.h: In instantiation of ‘struct std::__detail::_Hashtable_ebo_helper<0, bool(const std::tuple<float, float, float>&, const std::tuple<float, float, float>&), false>’:
/usr/include/c++/12/bits/hashtable_policy.h:1631:12: required from ‘struct std::__detail::_Hashtable_base<std::tuple<float, float, float>, std::tuple<float, float, float>, std::__detail::_Identity, bool(const std::tuple<float, float, float>&, const std::tuple<float, float, float>&), std::hash<std::tuple<float, float, float> >, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Hashtable_traits<true, true, true> >’
/usr/include/c++/12/bits/hashtable.h:182:11: required from ‘class std::_Hashtable<std::tuple<float, float, float>, std::tuple<float, float, float>, std::allocator<std::tuple<float, float, float> >, std::__detail::_Identity, bool(const std::tuple<float, float, float>&, const std::tuple<float, float, float>&), std::hash<std::tuple<float, float, float> >, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits<true, true, true> >’
/usr/include/c++/12/bits/unordered_set.h:100:18: required from ‘class std::unordered_set<std::tuple<float, float, float>, std::hash<std::tuple<float, float, float> >, bool(const std::tuple<float, float, float>&, const std::tuple<float, float, float>&)>’
unordered_set_triplet.cpp:21:26: required from here
/usr/include/c++/12/bits/hashtable_policy.h:1204:11: error: data member ‘std::__detail::_Hashtable_ebo_helper<0, bool(const std::tuple<float, float, float>&, const std::tuple<float, float, float>&), false>::_M_tp’ invalidly declared function type
1204 | _Tp _M_tp{};
| ^~~~~
/usr/include/c++/12/bits/hashtable.h: In instantiation of ‘class std::_Hashtable<std::tuple<float, float, float>, std::tuple<float, float, float>, std::allocator<std::tuple<float, float, float> >, std::__detail::_Identity, bool(const std::tuple<float, float, float>&, const std::tuple<float, float, float>&), std::hash<std::tuple<float, float, float> >, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits<true, true, true> >’:
/usr/include/c++/12/bits/unordered_set.h:100:18: required from ‘class std::unordered_set<std::tuple<float, float, float>, std::hash<std::tuple<float, float, float> >, bool(const std::tuple<float, float, float>&, const std::tuple<float, float, float>&)>’
unordered_set_triplet.cpp:21:26: required from here
/usr/include/c++/12/bits/hashtable.h:665:7: error: function returning a function
665 | key_eq() const
| ^~~~~~
In file included from /usr/include/c++/12/unordered_set:47:
/usr/include/c++/12/bits/unordered_set.h: In instantiation of ‘class std::unordered_set<std::tuple<float, float, float>, std::hash<std::tuple<float, float, float> >, bool(const std::tuple<float, float, float>&, const std::tuple<float, float, float>&)>’:
unordered_set_triplet.cpp:21:26: required from here
/usr/include/c++/12/bits/unordered_set.h:632:7: error: function returning a function
632 | key_eq() const
| ^~~~~~
unordered_set_triplet.cpp: In function ‘int main()’:
unordered_set_triplet.cpp:21:49: error: expected primary-expression before ‘,’ token
21 | unordered_set_triplet s(10, std::hash<triplet>, triplet_equal);
| ^
如何解决这个问题?
编辑
使用(已订购)也不起作用
set
:
>> cat set_triplet.cpp
#include <iostream>
#include <set>
#include <tuple>
#include <limits> // numeric_limits
#include <cmath> // fabs
#include <functional> // hash
using triplet = std::tuple<float, float, float>;
bool triplet_equal(triplet const & lhs, triplet const & rhs) {
float const eps = std::numeric_limits<float>::epsilon();
if (std::fabs(std::get<0>(lhs) - std::get<0>(rhs)) > eps) return false;
if (std::fabs(std::get<1>(lhs) - std::get<1>(rhs)) > eps) return false;
if (std::fabs(std::get<2>(lhs) - std::get<2>(rhs)) > eps) return false;
return true;
}
using set_triplet = std::set<triplet, std::hash<triplet>, decltype(triplet_equal)>;
int main() {
//set_triplet s; // Compilation: KO...
set_triplet s(10, std::hash<triplet>, triplet_equal);
s.insert({1.f, 2.f, 3.f});
s.insert({1.0000001f, 2.0000001f, 3.0000001f});
for (auto const & t : s) std::cout << std::get<0>(t) << ", " << std::get<1>(t) << ", " << std::get<2>(t) << std::endl;
}
什么容器适合使用?
triplet
可以看到一个3D点(XYZ):我需要处理/检测重复点。
首先,提供给 std::unordered_set
的
KeyEqual不能是一个函数,而这正是您想要对
decltype(triplet_equal)
执行的操作。但是,它可以是函数指针。通常,您应该按如下方式使用函数对象:
struct triplet_equal {
// note: static constexpr only since C++23, otherwise remove those two
static constexpr bool operator()(triplet const & lhs, triplet const & rhs) const {
float const eps = std::numeric_limits<float>::epsilon();
if (std::fabs(std::get<0>(lhs) - std::get<0>(rhs)) > eps) return false;
if (std::fabs(std::get<1>(lhs) - std::get<1>(rhs)) > eps) return false;
if (std::fabs(std::get<2>(lhs) - std::get<2>(rhs)) > eps) return false;
return true;
}
};
// ...
std::unordered_set<triplet, std::hash<triplet>, triplet_equal> s(10);
您不必为构造函数提供哈希或
triplet_equal
的任何值,因为它们是默认可构造的。
下一个巨大问题是标准库没有针对元组的
std::hash
专业化。如果您想创建自己的,请查看 unordered_map / unordered_set 中元组的通用哈希。
但是,即使有一个,问题仍然是两个相等的元组(其中相等性由 triplet_equal
确定)也必须具有相同的哈希值。您必须自己专门化
std::hash
,以便两个相等的元组始终具有相同的哈希值,尽管浮点不精确。您也许可以通过量化
float
来做到这一点,但我想正确地做到这一点非常困难。替代方案:使用
std::set
并提供Compare
std::set
会容易得多,它只需要你实现小于比较:
struct triplet_less {
constexpr bool operator()(triplet const & lhs, triplet const & rhs) const {
float const eps = std::numeric_limits<float>::epsilon();
float d0 = std::get<0>(lhs) - std::get<0>(rhs);
if (d0 < -eps) return true;
if (d0 > eps) return false;
float d1= std::get<1>(lhs) - std::get<1>(rhs);
if (d1 < -eps) return true;
if (d1 > eps) return false;
float d2 = std::get<2>(lhs) - std::get<2>(rhs);
if (d2 < -eps) return true;
return false;
}
};
int main() {
std::set<triplet, triplet_less> s;
s.insert({1.f, 2.f, 3.f});
}