< hash_set>等于运算符在VS2010中不起作用 [英] <hash_set> equality operator doesn't work in VS2010

查看:313
本文介绍了< hash_set>等于运算符在VS2010中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

示例代码:

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 doesn't stores elements in some order defined by hash function... why? Please note that this code works in VS2008 using stdext::hash_set.

推荐答案

看起来平等比较被打破对于Visual C ++ 2010中的 hash_set unordered_set

It looks like equality comparisons are broken for both hash_set and unordered_set in Visual C++ 2010.

我使用标准的语言为无序容器实现了一个朴素的平等函数引用由Matthieu 来验证它是一个错误(只是为了确保):

I implemented a naive equality function for unordered containers using the language from the standard quoted by Matthieu to verify that it's a bug (just to be sure):

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 hs2 是相等的。 (有人让我知道,如果你发现了一个错误的代码;我没有真正测试它广泛...)

It returns that hs1 and hs2 from your example are equal. (Somebody let me know if you spot a bug in that code; I didn't really test it extensively...)

我会提交一个缺陷报告Microsoft连接。

I'll file a defect report on Microsoft Connect.

这篇关于&lt; hash_set&gt;等于运算符在VS2010中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆