如何unordered_set <tuple <int,int >>?

yoc*_*oco 6 c++ boost

我在构造一个时遇到了奇怪的问题unordeed_set<tuple<int,int>>.我曾尝试过VC++ 8,gcc3.2,gcc4.3,都有相同的结果.我不知道代码有什么问题,以下是我的代码:

#include <boost/unordered_set.hpp>
#include <boost/tuple/tuple.hpp>
// For unordered container, the declaration of operator==
#include <boost/tuple/tuple_comparison.hpp>

using namespace std ;
using namespace boost ;

// define of the hash_value funciton for tuple<int, int>
size_t hash_value(tuple<int, int> const& t) {
    return get<0>(t) * 10 + get<1>(t) ;
}

int main () {
    unordered_set<tuple<int, int>> s ;
    tuple<int, int> t ;
    s.insert(t) ;
}
Run Code Online (Sandbox Code Playgroud)

这是编译错误消息:

1>c:\libs\boost_1_37_0\boost\functional\hash\extensions.hpp(72) : error C2665: 'boost::hash_value' : none of the 16 overloads could convert all the argument types
1>        c:\libs\boost_1_37_0\boost\functional\hash\hash.hpp(33): could be 'size_t boost::hash_value(bool)'
1>        c:\libs\boost_1_37_0\boost\functional\hash\hash.hpp(34): or       'size_t boost::hash_value(char)'
1>        c:\libs\boost_1_37_0\boost\functional\hash\hash.hpp(35): or       'size_t boost::hash_value(unsigned char)'
....
Run Code Online (Sandbox Code Playgroud)

看来编译器看不到定义了hash_value(tuple<int, int>).但是,如果我替换tuple<int, int>其他数据类型struct F{int a, b;},它的工作原理.那真的很奇怪.我想念什么吗?非常感谢你.

Bil*_*nch 6

将哈希函数放在命名空间boost中.

#include <boost/unordered_set.hpp>
#include <boost/tuple/tuple.hpp>
#include <boost/tuple/tuple_comparison.hpp>

using namespace std;
using namespace boost;

namespace boost {
    size_t hash_value(tuple<int, int> const & t) {
        return get<0>(t) * 10 + get<1>(t) ;
    }
}

int main () {
    unordered_set< tuple<int, int> > s ;
    tuple<int, int> t ;
    s.insert(t) ;
}
Run Code Online (Sandbox Code Playgroud)