In my current homework assignment, I created an iterator class for a list. I am stuck in creating a good function erase(iterator where).
Current code (reduced to question):
class List
{
class _Iter
{
friend class List;
public:
_Iter(ListElem *pCurr, List *pList);
private:
ListElem *pCurr_; List *pList_;
};
typedef _Iter iterator;
iterator erase(iterator where);
};
when erasing is done like this:
List::iterator List::erase(List::iterator& where)
{
if(where == end() && where == begin())
{
pop_back();
return iterator(0, this);
}
if(where == end())
{
pop_back();
return end();
}
else
{
if(where == begin())
{
pop_front();
return ++begin();
}
}
iterator temp(where);
where.pCurr_->next->prev = where.pCurr_->prev;
where.pCurr_->prev->next = where.pCurr_->next;
++temp;
delete where.pCurr_;
--size_;
return temp;
}
The first three cases: only the element, the element at the end and the element at the beginning - everything is in order. Coded fine and requires absolutely no knowledge and personal access to members _Iter. However, if the item is not in these positions, then I (apparently) have no choice but to break encapsulation and directly modify pCurr_ (list item).
? STL, _Next_Node_(/* stuff */) _Prev_Node_(/* stuff */), . Google , , .
: , , pCurr_?