Boost logo

Boost :

From: Markus Pesti (news1_at_[hidden])
Date: 2003-01-15 11:32:28


I can't find any good documentation about boost::mpl::find_if. The syntax is
different to that of std::find_if and std::find_if cannot be applied to
lists of boost::shared_ptr's. So I created my own find_if by modifying the
std one.:

<code>
#include <iostream>
#include <list>
#include <boost/shared_ptr.hpp>

namespace my {
template <class InputIterator, class Predicate>
InputIterator find_if (InputIterator first, InputIterator last, Predicate
pred)
{
 while (first != last && !pred(first->get())) ++first;
 return first;
}
};

class A {
int x;
public:
 A(int x) : x(x) {}
 bool operator() (const A* a) { return a->x == x; }
};

void fill(std::list<boost::shared_ptr<A> >& Alist, int n) {
 for(int i=0; i<n; i++)
  Alist.push_back(boost::shared_ptr<A>(new A(i)));
}

bool find(const std::list<boost::shared_ptr<A> >& Alist, int v) {
 return my::find_if(Alist.begin(), Alist.end(), A(v)) != Alist.end();
}

int main() {
 std::list<boost::shared_ptr<A> > As;
 fill(As, 10);
 if(find(As, 5))
  std::cout << "5 found.\n";
 else
  std::cout << "5 not found.\n";
 if(find(As, 15))
  std::cout << "15 found.\n";
 else
  std::cout << "15 not found.\n";
}
</code>

That works fine, but I don't know whether that is o.k. to do so. Or is there
a better way?

Markus


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