如何插入 boost::unordered_set<boost::unordered_set<int> >?
How do I insert into boost::unordered_set<boost::unordered_set<int> >?
以下代码无法编译,但如果我删除注释行,它会正确编译并运行。我只是打算使用 boost,因为 C++ 默认不为 std::unordered_set<int>
提供散列函数。
#include <iostream>
#include <boost/unordered_set.hpp>
int main() {
boost::unordered_set<boost::unordered_set<int> > fam;
boost::unordered_set<int> s;
s.insert(5);
s.insert(6);
s.insert(7);
std::cout << s.size() << std::endl;
fam.insert(s); // this is the line causing the problem
return 0;
}
编辑 1:
我想比在 OP 中更清楚。首先我知道 boost::unordered_set<>
的想法是它是用散列 table 而不是 BST 实现的。我知道任何要成为 boost::unordered_set<>
模板类型的东西都需要提供散列函数和相等函数。我也知道默认情况下 std::unordered_set<>
没有定义哈希函数,这就是以下代码无法编译的原因:
#include <iostream>
#include <unordered_set>
int main() {
std::unordered_set<std::unordered_set<int> > fam;
return 0;
}
但是,我认为 boost 为它们的所有容器提供哈希函数,这就是为什么我相信以下代码 编译:
#include <iostream>
#include <boost/unordered_set.hpp>
int main() {
boost::unordered_set<boost::unordered_set<int> > fam;
return 0;
}
所以现在,我不确定为什么上面的 boost 代码可以编译,而 OP 中的代码却不能。 boost 为所有容器提供哈希函数是我错了吗?我真的很想避免必须定义一个新的散列函数,特别是当我的实际用途是有一个更复杂的数据结构时:boost::unordered_map<std::pair<boost::unordered_map<int, int>, boost::unordered_map<int, int> >, int>
。这似乎应该是一个解决的问题,我不必自己定义,因为 IIRC python 可以毫无问题地处理集合。
unordered_set
(或_map
)使用散列,并且需要为其元素定义散列运算符。没有为 boost::unordered_set<int>
定义哈希运算符,因此它不能将这种类型的元素放入您的集合中。
您可以为此编写自己的哈希函数。例如,这是一种典型的通用散列方法,但您可能希望针对您的特定数据对其进行自定义。如果将此代码放入您的示例中,它应该可以工作:
namespace boost {
std::size_t hash_value(boost::unordered_set<int> const& arg) {
std::size_t hashCode = 1;
for (int e : arg)
hashCode = 31 * hashCode + hash<int>{}(e);
return hashCode;
}
}
以下代码无法编译,但如果我删除注释行,它会正确编译并运行。我只是打算使用 boost,因为 C++ 默认不为 std::unordered_set<int>
提供散列函数。
#include <iostream>
#include <boost/unordered_set.hpp>
int main() {
boost::unordered_set<boost::unordered_set<int> > fam;
boost::unordered_set<int> s;
s.insert(5);
s.insert(6);
s.insert(7);
std::cout << s.size() << std::endl;
fam.insert(s); // this is the line causing the problem
return 0;
}
编辑 1:
我想比在 OP 中更清楚。首先我知道 boost::unordered_set<>
的想法是它是用散列 table 而不是 BST 实现的。我知道任何要成为 boost::unordered_set<>
模板类型的东西都需要提供散列函数和相等函数。我也知道默认情况下 std::unordered_set<>
没有定义哈希函数,这就是以下代码无法编译的原因:
#include <iostream>
#include <unordered_set>
int main() {
std::unordered_set<std::unordered_set<int> > fam;
return 0;
}
但是,我认为 boost 为它们的所有容器提供哈希函数,这就是为什么我相信以下代码 编译:
#include <iostream>
#include <boost/unordered_set.hpp>
int main() {
boost::unordered_set<boost::unordered_set<int> > fam;
return 0;
}
所以现在,我不确定为什么上面的 boost 代码可以编译,而 OP 中的代码却不能。 boost 为所有容器提供哈希函数是我错了吗?我真的很想避免必须定义一个新的散列函数,特别是当我的实际用途是有一个更复杂的数据结构时:boost::unordered_map<std::pair<boost::unordered_map<int, int>, boost::unordered_map<int, int> >, int>
。这似乎应该是一个解决的问题,我不必自己定义,因为 IIRC python 可以毫无问题地处理集合。
unordered_set
(或_map
)使用散列,并且需要为其元素定义散列运算符。没有为 boost::unordered_set<int>
定义哈希运算符,因此它不能将这种类型的元素放入您的集合中。
您可以为此编写自己的哈希函数。例如,这是一种典型的通用散列方法,但您可能希望针对您的特定数据对其进行自定义。如果将此代码放入您的示例中,它应该可以工作:
namespace boost {
std::size_t hash_value(boost::unordered_set<int> const& arg) {
std::size_t hashCode = 1;
for (int e : arg)
hashCode = 31 * hashCode + hash<int>{}(e);
return hashCode;
}
}