help-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: need help with stl lists


From: Christian Christmann
Subject: Re: need help with stl lists
Date: Fri, 20 May 2005 10:38:17 +0200

Thank you for your answer.

> In looking at your attempt to use std::list, these are my observations:
> 
> 1.  Use of find.
> 
> Since you are storing pointers in the container, using find will compare
> pointers.  Is this what you want?  I expect that you really want to
> compare the objects that are pointed to.  To do this, you need to use
> find_if and write a compare functor that dereferences the pointers.

For example, in my code the list stores pointers to objects of the class
Node. The Node class looks like:

template <class NODE>
class Node
{
        NODE* info;
        [snip]
}

In the source code of node.cpp I've an operator defined as:

template <class NODE>
bool Node<NODE>::operator==(const Node& node) const
{
    return (*info == *(node.info));
}

So, when "find" is used in the list, it should compare the
objects that are pointed to. Is this OK or do I really have to
use find_if.

> 
> 2.  The return value of Succ.
> 
> Once you obtain an iterator to the item you are searching for, you
> pre-increment it prior to dereferencing in the return statement.  This is
> wrong, no increment is needed.

The function Succ has to return a successor of ELEMENT* x.
So after obtaining an iterator to the item I'm searching for I've to
increment it to get the successor of the found item and return
the incremented value. If I'm wrong, please tell my why.

> 
> 3.  Why a pointer?
> 
> I can understand the need to store pointers in a container, but not having
> a dynamically generated container.  Why not simply have a by-value Has-A
> relationship between your class and the std::list?

Actually, you are right. I'm going to change the pointer to a by-value
relationship.

> 
> By the way, using boost::indirect_iterator can make using a container of
> pointers much easier to work with.
> 
> see: http://www.boost.org/libs/iterator/doc/indirect_iterator.html

I will have a look on the boost iterators.

Chris


reply via email to

[Prev in Thread] Current Thread [Next in Thread]