By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,915 Members | 1,335 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,915 IT Pros & Developers. It's quick & easy.

successor function for STL lists

P: n/a
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
Jul 23 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Christian Christmann wrote:
I've some old code which I would like to replace by STL.
Instead of attempting a piece-by-piece replacement, and mostly because
the functionality is not compatible, you'd be better off rewriting the
whole thing using iterators (and templatizing it while reimplementing).
[...]


V
Jul 23 '05 #2

P: n/a
> 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).
A few points:

1) Your class is intrusive in that it Element defines pointers to
previous/next elements.
std::list is non-intrusive. The redesigned class would be

class Element
{
// some data types
};

and therefore this newly designed Element could be placed in any of the
containers, not just linked lists. std::list supplies the pointers to
next/previous elements not class Element.

2) There is no such thing as "the current element" for std::list.

As Victor says, I would replace the equivalent functionality with std::list
and not seek to implement some kind of auxiliary functions to std::list that
basically replicate your old class.

Stephen Howe


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

Jul 23 '05 #3

P: n/a
On Thu, 12 May 2005 12:36:01 -0400, Victor Bazarov wrote:
Christian Christmann wrote:
I've some old code which I would like to replace by STL.
Instead of attempting a piece-by-piece replacement, and mostly because the
functionality is not compatible, you'd be better off rewriting the whole
thing using iterators (and templatizing it while reimplementing).


Generally I agree with you. But the problem is, that the functions of the
list class are used frequently in some other programs. Thus, when I
just replace the body of the function and keep the prototype definition
of that function I don't need to change all the programs using the
list class.
[...]


V


Jul 23 '05 #4

P: n/a
Christian Christmann wrote:
On Thu, 12 May 2005 12:36:01 -0400, Victor Bazarov wrote:
Christian Christmann wrote:
I've some old code which I would like to replace by STL.


Instead of attempting a piece-by-piece replacement, and mostly because
the functionality is not compatible, you'd be better off rewriting the
whole thing using iterators (and templatizing it while reimplementing).


Generally I agree with you. But the problem is, that the functions of the
list class are used frequently in some other programs. Thus, when I
just replace the body of the function and keep the prototype definition
of that function I don't need to change all the programs using the
list class.


So, if you insist, what about something like this:
class bad_list {
private:

typedef void* void_p;
typedef std::list< void_p > void_p_list;

void_p_list the_list;
void_p_list::iterator the_cursor;

public:

void_p NextElement ( void ) {
if ( the_cursor == the_list.end() ) {
return ( 0 );
}
void_p result = *the_cursor;
++ the_cursor;
return( result );
}

};

Best

Kai-Uwe Bux
Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.