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 21:26:28 +0200

Hi,

thank you for helping me.

> 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));
>      }
>      }
>    }

Just to make sure:
I've to add the above posted operator to my class Node
and than use "find_if" in the class List. For example in the
Delete function:

template <class ELEMENT>
void List<ELEMENT>::Delete(ELEMENT* x)
{
   typename list<ELEMENT*>::iterator it = find_if(mylist.begin(), 
    mylist.end(), x);
  if (it != mylist.end())
    mylist.erase(it);      
}

Is that right?

However, could you explain to me why to use find_if instead of find?
When I use find and define operators != and == (where I dereferences the
pointers) in the Node class as posted last time, does find not compare
the object?


> 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.

Unfortunately, the elements in the container are not sorted.


> 
> 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.


Thank you for your advice. Yes, I didn't care about that but in the Succ
function the iterator can indeed point to end(). I've to correct that.

Chris


reply via email to

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