|
Boost : |
Subject: Re: [boost] Boost Iterator Library - small extension proposal
From: Nathan Ridge (zeratul976_at_[hidden])
Date: 2011-11-30 15:04:33
> I used sometimes the Boost.Iterator library - especially the filter
> iterators. I like them much, but I don't like the work to create the
> iterator objects.
>
> ---
>
> Example:
>
> struct IsEven
> {
> inline bool operator()(int n) const
> {
> return n%2==0;
> }
> };
>
> vector<int> v;
>
> IsEven iseven;
> filter_iterator<IsEven, vector<int>::iterator>
> filter_iter_begin(iseven, v.begin(), v.end());
> filter_iterator<IsEven, vector<int>::iterator>
> filter_iter_end(iseven, v.end(), v.end());
>
> ---
>
> [snip]
>
> Usage:
>
> vector<int> v;
> auto iters = make_filter_iterators(
> v.begin(), v.end(), [](int n){ return n%2==0; });
> copy(iters.first, iters.second, ostream_iterator<int>(cout, " "));
Have you tried the Boost.Range library? With that you can do simply
vector<int> v;
copy(v | filtered(IsEven()), ostream_iterator<int>(cout, " "));
That works even in C++03. Of course, in C++11 you can do:
vector<int> v;
copy(v | filtered([](int n){ return n%2==0; }), ostream_iterator<int>(cout, " "));
Regards,
Nate
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk