Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 309
Frank Avatar asked Apr 25 '09 23:04

Frank


2 Answers

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.

like image 186
rlbond Avatar answered Nov 11 '22 22:11

rlbond


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.

like image 31
Benoît Avatar answered Nov 12 '22 00:11

Benoît