gpt4 book ai didi

c++ - 如何unordered_set>?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:29:36 24 4
gpt4 key购买 nike

我在构建 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) ;
}

这是编译错误信息:

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)'
....

编译器似乎看不到hash_value(tuple<int, int>)的定义.但是如果我更换 tuple<int, int>到其他数据类型,如 struct F{int a, b;}它有效。这真的很奇怪。我想念什么吗?非常感谢。

最佳答案

将哈希函数放在命名空间 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) ;
}

关于c++ - 如何unordered_set<tuple<int,int>>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1250599/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com