示例代码:
std::hash_set<int> hs1; // also i try std::unordered_set<int> - same effect
std::hash_set<int> hs2;
hs1.insert(15);
hs1.insert(20);
hs2.insert(20);
hs2.insert(15);
assert(hs1 == hs2);
hash_set 不按哈希函数定义的某种顺序存储元素...为什么?
请注意,此代码在 VS2008 中使用 stdext::hash_set 运行。
看起来两者的相等比较都被打破了hash_set
and unordered_set
在 Visual C++ 2010 中。
我使用标准语言为无序容器实现了一个朴素的相等函数马蒂厄引用 https://stackoverflow.com/questions/2772742/hash-set-equality-operator-doesnt-work-in-vs2010/2772880#2772880验证它是否是一个错误(只是为了确定):
template <typename UnorderedContainer>
bool are_equal(const UnorderedContainer& c1, const UnorderedContainer& c2)
{
typedef typename UnorderedContainer::value_type Element;
typedef typename UnorderedContainer::const_iterator Iterator;
typedef std::pair<Iterator, Iterator> IteratorPair;
if (c1.size() != c2.size())
return false;
for (Iterator it(c1.begin()); it != c1.end(); ++it)
{
IteratorPair er1(c1.equal_range(*it));
IteratorPair er2(c2.equal_range(*it));
if (std::distance(er1.first, er1.second) !=
std::distance(er2.first, er2.second))
return false;
// A totally naive implementation of is_permutation:
std::vector<Element> v1(er1.first, er1.second);
std::vector<Element> v2(er2.first, er2.second);
std::sort(v1.begin(), v1.end());
std::sort(v2.begin(), v2.end());
if (!std::equal(v1.begin(), v1.end(), v2.begin()))
return false;
}
return true;
}
它返回的是hs1
and hs2
从你的例子来看是平等的。 (如果您发现该代码中的错误,请告诉我;我并没有真正对其进行广泛测试......)
我将在 Microsoft Connect 上提交缺陷报告。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)