Boost logo

Boost Users :

From: Matthias Kaeppler (nospam_at_[hidden])
Date: 2005-03-01 04:31:34


David Abrahams wrote:
> Careful; vector iterators are not neccessarily pointers.

Hm yes, that's true. But does this matter? I have a vector of some type,
and a vector of pointers to some type. I don't even deal with iterators
when working on the pointer-vector.

> struct indirect_less
> {
> template <class It>
> bool operator()(It i1, It i2) const
> {
> typedef typename std::iterator_traits<It>::value_type value_type;
> return std::less<value_type>()(i1,i2);
> }
> };
>
> sort(ptrcoll.begin(), ptrcoll.end, indirect_less());
>

Ah I see. But what, if I want to abstract from the predicate? I want to
create a class which is as generic as possible, so I don't have to
rewrite it for each and every possible predicate.

What if I want to pass the predicate to the constructor of this
template. For one, passing a pointer to a function returning bool and
taking on argument must be possible, and also passing a function object,
maybe of type std::unary_function.

template<typename It>
class indirect
{
    const std::unary_function<It,bool>& fctor_;
public:
    indirect( const std::unary_function<It,bool>& fctor ): fctor_(fctor) {}
    bool operator()(It i1, It i2) const
    {
        typedef typename std::iterator_traits<It>::value_type value_type;
        return fctor_<It,bool>()(i1,i2);
    }
};

Would that work?

-- 
Matthias Kaeppler

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net