Boost logo

Boost :

From: Jeff Paquette (paquette_at_[hidden])
Date: 2000-10-17 18:29:24


Hi,

I've been trying to work out a general method for using STL-style algorithms
on containers of pointers. I have a solution that actually compiles and
works on GCC and VC6sp4, but I'm not too fond of the syntax (or the
formatting!):

template<typename _Pred> struct pointer_proxy2 :
std::binary_function<typename _Pred::result_type, typename
_Pred::first_argument_type, typename _Pred::second_argument_type>
{
  _Pred Pr;
  pointer_proxy2(_Pred pr) : Pr(pr) {}
  inline typename _Pred::result_type operator()(const typename
_Pred::first_argument_type *f, const typename _Pred::second_argument_type
*s) const {
    return Pr(*f, *s);
  }
};

template<typename _Pred> struct pointer_proxy1 :
std::unary_function<typename _Pred::result_type, typename
_Pred::argument_type>
{
  _Pred Pr;
  pointer_proxy1(_Pred pr) : Pr(pr) {}
  inline typename _Pred::result_type operator()(const typename
_Pred::argument_type *f) const {
    return Pr(*f);
  }
};

int main(int, char **) {
    std::vector<int *> v;

    for (int i(0); i < 20; ++i)
      v.push_back(new int(20 - i));

    int target = *v.front();

    std::sort(v.begin(), v.end(),
              pointer_proxy2<std::less<int> >(std::less<int>()));

    std::vector<int *>::iterator p;
    p = std::find_if(v.begin(), v.end(),
                     pointer_proxy1<std::binder2nd<std::equal_to<int> >
>(std::bind2nd(std::equal_to<int>(), target)));
    if (p != v.end())
      std::cout << "Value " << target << " was found " <<
std::distance(v.begin(), p) << " entries into the list\n";
    else
      std::cout << "Value " << target << " not found ";

  return 0;
  }

What I'm not wild about is having to explicitly provide the type to the
template (what happened to template-argument deduction? Does it not apply
here?).

Can anyone suggest any improvements?

--
Jeff Paquette
paquette at mediaone.net
http://www.atnetsend.net

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk