Making a hash table of iterators in C++
Use the address of the element that the iterator refers to.
struct list_iterator_hash {
size_t operator()(const list<int>::iterator &i) const {
return hash<int*>()(&*i);
}
};
But this will only work for dereferenceable iterators, not end()
or list<int>::iterator()
.
You can use pointers to the element in place of iterators. Let's say you had a list of structs MyStruct
. You can use
unordered_set<MyStruct*> myhashset;
and the C++ standard library already implements std::hash
for any pointer.
So if you ever need to insert or search listIt
then use &(*listIt)
which will get the pointer of type MyStruct*
.