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: Jeffrey Holle
Subject: Re: need help with stl lists
Date: Fri, 20 May 2005 09:03:14 -0500
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.6) Gecko/20040115

Yes, it is necessary to use find_if if you want to compare objects when using a container of pointers. Also, this is the predicate that maps to the code snibit that you provide:
  template <class NODE>
  class PtrCompare
  {
    bool operator()(const NODE& node) const
    {
      return (*info == *(node.info));
    }
  }

As for the pre-increment of the iterator, I don't know your design, so I can't say its wrong, but it doesn't seem right either. It implies that you have an order to your container. If this is so, why not use a std::set? This would let you use the find method of set instead of find_if, though you would have to code a predicate that implements operator<, like:
  template <class NODE>
  class PtrCompare
  {
    bool operator()(const NODE& node) const
    {
      return (*info < *(node.info));
    }
  }

Doing this would make your application much more efficient because a linear search is avoided.

Another issue is what about finding the end of the container? Incementing the iterator at this point would point to end() which is past the last element of your container.

Christian Christmann wrote:
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:



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]