C
Christian Christmann
Hi,
I've some old code which I would like to replace by STL.
It's a self-written double linked list which consists of instances
of another class Element which contains the information as
void* (I'm going to replace the void* by a template but that's
not an issue here).
The class Element is defined as:
class Element
{
void* value;
Element* nextelem, *prevelem;
[snip]
};
The class List is defined as:
class List
{
private:
Element* first;
Element* last;
// used as forward iterator
Element* forward_iterator ;
// used as backward iterator
Element* backward_iterator;
[snip]
public:
void* NextElement();
[snip]
};
and the function NextElement() is defined as:
void* List::NextElement()
{
// isEmpty() checks wheather list empty
if (!forward_iterator || isEmpty())
return NULL;
// value is the void* content of Element
void *next = forward_iterator->value;
forward=forward->nextelem;
return next;
}
What would be the best and most efficient way to replace
this function by a function using STL lists?
If the function NextElement would require an Element
as parameter to find its successor like
void* List::NextElement(void* currentElement)
I would first use the algorithm find() and than
increment the iterator. Something like
return *(++(find(begin(), end(), currentElement)));
(hope the syntax is ok )
Thank you for your help in advance
Regards,
Chris
I've some old code which I would like to replace by STL.
It's a self-written double linked list which consists of instances
of another class Element which contains the information as
void* (I'm going to replace the void* by a template but that's
not an issue here).
The class Element is defined as:
class Element
{
void* value;
Element* nextelem, *prevelem;
[snip]
};
The class List is defined as:
class List
{
private:
Element* first;
Element* last;
// used as forward iterator
Element* forward_iterator ;
// used as backward iterator
Element* backward_iterator;
[snip]
public:
void* NextElement();
[snip]
};
and the function NextElement() is defined as:
void* List::NextElement()
{
// isEmpty() checks wheather list empty
if (!forward_iterator || isEmpty())
return NULL;
// value is the void* content of Element
void *next = forward_iterator->value;
forward=forward->nextelem;
return next;
}
What would be the best and most efficient way to replace
this function by a function using STL lists?
If the function NextElement would require an Element
as parameter to find its successor like
void* List::NextElement(void* currentElement)
I would first use the algorithm find() and than
increment the iterator. Something like
return *(++(find(begin(), end(), currentElement)));
(hope the syntax is ok )
Thank you for your help in advance
Regards,
Chris