Dar*_*der 8 c++ hash iterator c++11
我试图通过散列一些节点指针来加速特定的链表操作.这是我正在使用的代码:
unordered_set< typename list< int >::iterator > myhashset;
Run Code Online (Sandbox Code Playgroud)
在Visual Studio 2012中,我收到"错误C2338:C++标准不提供此类型的哈希",因为编译器不知道如何散列迭代器.因此,我需要为列表迭代器实现我自己的哈希函数,如下所示:
struct X{int i,j,k;};
struct hash_X{
size_t operator()(const X &x) const{
return hash<int>()(x.i) ^ hash<int>()(x.j) ^ hash<int>()(x.k);
}
};
Run Code Online (Sandbox Code Playgroud)
(维基百科参考)
我无法弄清楚迭代器的哪些成员保证唯一性(因此,我想要哈希的成员).另一个问题是那些成员可能是私人的.
想到的一个解决方案是重新实现和list :: iterator,但这看起来像是一个hack并引入了更多的代码来维护.
使用迭代器引用的元素的地址.
struct list_iterator_hash {
size_t operator()(const list<int>::iterator &i) const {
return hash<int*>(&*i);
}
};
Run Code Online (Sandbox Code Playgroud)
但这只适用于可解除引用的迭代器,而不是end()或list<int>::iterator().