Frank
Frank

Reputation: 66154

boost::filter_iterator -- how would I do that with the STL?

I am passed an Iterator and I have to pass it on to another function -- but filtered so that certain elements are skipped (it's a range of pointers, and I want to filter out the NULL pointers).

I googled for "stl filter iterator" to see how to do this, and boost::filter_iterator came up.

That looks nice and I could use it, but could I do that with the good old STL as well? Without copying the elements into a new container, of course.

I guess I'd have to create another iterator class that provides the necessary begin(), end() etc functions and does the filtering? So I'd exactly have to reimplement the boost iterator_filter...?

Upvotes: 11

Views: 2952

Answers (2)

Benoît
Benoît

Reputation: 16994

I agree with rlbond. Don't reinvent the wheel and use Boost::filter_iterator. If you don't want to have all boost libraries in your project, you may want to consider using boost.bcp to extract exactly the ones you need.

Upvotes: 4

rlbond
rlbond

Reputation: 67749

You are correct; you would essentially be recreating the filter iterator yourself. My advice would be to use Boost's filter_iterator. Boost has special status as c++'s most used external library; many c++ committee members have helped write libraries for boost. Its ubiquity essentially makes it almost-stl as is; there's really no reason to reinvent the wheel here.

Upvotes: 8

Related Questions