C
Chris Schadl
Hi,
I've written a simple sorted linked list class that I'm trying to
implement an iterator for. I'm trying to make the interface to the
iterator simmilar to the STL's iterator interface, so one could do:
SortedList<int> sl;
SortedList<int>::iterator i;
for (i = sl.begin() ; i != sl.end() ; ++) { /* ... */ }
Right now I'm kind of stuck with the following code:
template <typename T>
class SortedList
{
private:
// To make life easier, our sorted list will be doubly-linked
struct ListNode {
ListNode *next;
ListNode *prev;
T data;
ListNode() { next = prev = NULL; }
} *head, *tail;
int size;
protected:
ListNode* _find(const T&) const;
public:
SortedList(); // Default constructor
SortedList(const SortedList&); // Copy constructor
virtual ~SortedList(); // Destructor
// ... Other methods (insert(), remove(), etc...
class _SortedListIterator {
private:
ListNode *cur; // Current element in list
public:
_SortedListIterator() : cur(NULL) { }
_SortedListIterator(ListNode *pos) : cur(pos) { }
T& operator*();
void operator++(); // Prefix
void operator++(int);// Postfix
friend bool operator==(_SortedListIterator&, _SortedListIterator&);
friend bool operator!=(_SortedListIterator&, _SortedListIterator&);
};
typedef _SortedListIterator iterator;
iterator begin() { iterator iter(head); return iter; }
iterator end() {iterator iter(NULL); return iter; }
};
Now, of course this dosen't even compile, but hopefully it kind of
illustrates what I'm trying to do. I'm not entierly certian if I should
declare _SotredListIterator as a template class, or if I did so, how it
would effect the _SortedListIterator typedef (AFAIK, typedef's cannot be
used with template typenames). Also, would I have establish friendship
between SortedList and _SortedListIterator so _SortedListIterator could
access SortedLists private members?
Thanks,
Chris Schadl
I've written a simple sorted linked list class that I'm trying to
implement an iterator for. I'm trying to make the interface to the
iterator simmilar to the STL's iterator interface, so one could do:
SortedList<int> sl;
SortedList<int>::iterator i;
for (i = sl.begin() ; i != sl.end() ; ++) { /* ... */ }
Right now I'm kind of stuck with the following code:
template <typename T>
class SortedList
{
private:
// To make life easier, our sorted list will be doubly-linked
struct ListNode {
ListNode *next;
ListNode *prev;
T data;
ListNode() { next = prev = NULL; }
} *head, *tail;
int size;
protected:
ListNode* _find(const T&) const;
public:
SortedList(); // Default constructor
SortedList(const SortedList&); // Copy constructor
virtual ~SortedList(); // Destructor
// ... Other methods (insert(), remove(), etc...
class _SortedListIterator {
private:
ListNode *cur; // Current element in list
public:
_SortedListIterator() : cur(NULL) { }
_SortedListIterator(ListNode *pos) : cur(pos) { }
T& operator*();
void operator++(); // Prefix
void operator++(int);// Postfix
friend bool operator==(_SortedListIterator&, _SortedListIterator&);
friend bool operator!=(_SortedListIterator&, _SortedListIterator&);
};
typedef _SortedListIterator iterator;
iterator begin() { iterator iter(head); return iter; }
iterator end() {iterator iter(NULL); return iter; }
};
Now, of course this dosen't even compile, but hopefully it kind of
illustrates what I'm trying to do. I'm not entierly certian if I should
declare _SotredListIterator as a template class, or if I did so, how it
would effect the _SortedListIterator typedef (AFAIK, typedef's cannot be
used with template typenames). Also, would I have establish friendship
between SortedList and _SortedListIterator so _SortedListIterator could
access SortedLists private members?
Thanks,
Chris Schadl