我不理解正确的东西。我的印象是unordered_set不会允许基于它们的散列的重复元素。std :: unordered_set允许插入重复记录
我有一个结构,其中的std ::散列,这似乎允许重复的特殊化,虽然我已经手动检查它
AddEdge (const std::shared_ptr<Relation> relation, const std::shared_ptr<Concept> concept)
{
auto edge = std::make_shared<Edge>((Edge){ relation, concept });
auto res = _edges.insert (edge);
return res.second;
}
一个重载函数不完全相同,但对于反转参数
这是边沿结构被散列:
namespace std
{
template<> struct hash<shared_ptr<Edge>>
{
size_t operator()(const shared_ptr<Edge> & ptr) const
{
size_t from = 0;
size_t to = 0;
if (auto node = std::dynamic_pointer_cast<Concept>(ptr->from))
from = hash<shared_ptr<Concept>>()(node);
else if (auto node = std::dynamic_pointer_cast<Relation>(ptr->from))
from = hash<shared_ptr<Relation>>()(node);
if (auto node = std::dynamic_pointer_cast<Concept>(ptr->to))
to = hash<shared_ptr<Concept>>()(node);
else if (auto node = std::dynamic_pointer_cast<Relation>(ptr->to))
to = hash<shared_ptr<Relation>>()(node);
return hash<size_t>()(from + to);
}
};
}
而且在举行的容器:
std::unordered_set<std::shared_ptr<Edge>> _edges;
当我这样做:
graph2->AddEdge(sea_node, is_node);
graph2->AddEdge(is_node, blue_node);
我得到:
Edge [sea,is] hash = 10017731961838884781
Edge [is,blue] hash = 11178184051384786808
我尝试第二次完全一样,我也得到相同的哈希值,但是,当我检查的边缘,我现在有4条边而不是2条。
我在做什么错?
编辑:类概念&关系有同样的散列函数:
namespace std
{
template<> struct hash<shared_ptr<Concept>>
{
size_t operator()(const shared_ptr<Concept> & ptr) const
{
return hash<string>()(ptr->asToken()->value()) + hash<int>()(ptr->TokenIndex()) + hash<string>()("Concept");
}
};
}
更interestignly,当我从加边我的输出,产生相同的哈希值,但它的重复边缘添加。
请看[testcase](http://sscce.org)。 – 2014-10-16 23:17:23
我哈希指针具有完全相同的哈希函数(请参阅编辑)。不幸的是,我不能创建一个测试用例,太多的类涉及一个小型自包含示例。 – 2014-10-16 23:21:07
创建测试用例的关键是_abstract away_或_remove_这些类。除非您的问题依赖于高度本地化的因素,例如特殊硬件或疯狂的海森堡,否则构建一个小型自包含示例绝不是不可能的。你应该把它作为你自己调试的第一步之一,早在寻求帮助之前......我们甚至你怎么知道所有那些“太多的类”都不会导致问题? – 2014-10-16 23:23:01