I'm trying to understand how the iterator works.
I have build the following code:
list<Bucket<Key,Val>> *array;
template<typename Key, typename Val>
class Bucket
{
public:
Bucket(Key key, Val value)
{
this->key = key;
this->value = value;
}
~Bucket(){}
const Key getKey() const
{
return key;
}
const Val getValue() const
{
return value;
}
private:
Key key;
Val value;
};
After inserting a bucket in one of the lists, I would like to do 2 actions at least: Find and Remove.
I can look for a given value with this code:
int entryNum = HashFunc()(key);
for(auto i: array[entryNum % capacity])
{
if(MatchFunc(i.getKey(),key)())
{
value = i.getValue();
}
}
But I can't seem to delete an item by adding something like i = array[entryNum % capacity].erase(i); or with remove. But if i do this, it works:
int entryNum = HashFunc()(key);
typename list<Bucket<Key,Val>>::iterator it;
for(auto i: array[entryNum % capacity])
{
++testcounter;
if(MatchFunc(i.getKey(),key)())
{
value = i.getValue();
}
}
for (it=array[entryNum % capacity].begin(); it!= array[entryNum % capacity].end(); ++it)
{
if(testcounter!=0) --testcounter;
else
{
array[entryNum % capacity].erase(it);
return htKeyFound;
}
}
This is obviously not ideal, I would like to NOT go twice over the same list + keeping a counter for it. But I can't seem to join them into one. When I try to access the it.getKey() it obviously doesn't work and crests a compilation error. Any advice about what I'm doing wrong?
Aucun commentaire:
Enregistrer un commentaire