I have a data structure that is essentially a hash table. The keys and values are stored separately for cache reasons. I have an iterator that traverses the container and returns pairs of keys and values when dereferenced.
However, I'm having a bit of trouble getting the iterator to behave like others. In particular with operator->. Here's what I have so far:
struct KeyValuePair
{
KeyValuePair(const int& key, int& value) : key(key), value(value) {}
const int& key;
int& value;
};
struct Iterator
{
KeyValuePair operator*() { return KeyValuePair(*keys, *values); }
// TODO: Implement this
//KeyValuePair* operator->() { return ... }
int* keys = nullptr;
int* values = nullptr;
};
This works fine for range-for and explicitly dereferencing the iterator
auto it = container.begin();
KeyValuePair pair = *it;
But it doesn't work for 'reaching through' the iterator because I don't have operator->
auto it = container.begin();
int& value = it->value;
And I can't figure out how to implement operator-> for this iterator. My first thought was to stick a KeyValuePair in the iterator and return a pointer to that, but a reference can't be reseated without shenanigans.
Any hints from folks more clever than myself?