Boost logo

Boost Users :

From: Ben Hutchings (ben.hutchings_at_[hidden])
Date: 2003-09-15 07:16:36


Witz <witz_at_[hidden]> wrote:
> You can find an implementation of copy_if in Boost Wiki here:
>
> http://www.crystalclearsoftware.com/cgi-bin/boost_wiki/wiki.pl
> ?STLAlgorithmExtensions
>
> An extension that i find useful is the following:
>
> template<class II, class OI, class PRED>
> std::pair<II,OI> copy_if(II ibegin, II iend, OI obegin, OI oend,
> PRED p)
> {
> for(; ibegin != iend; ++ibegin)
> {
> if(p(*ibegin))
> {
> *obegin = *ibegin;
> if(++obegin == oend)
> break;
> }
> }
> return std::make_pair(ibegin, obegin);
> }
>
> Here we have a finite sized sink. Items are copied from the source if
> they satisfy the condition and if there is room left in the sink. The
> positions reached are returned on termination of the algorithm.

This should check whether obegin == end before copying, in case obegin
== oend initially. I don't know whether it should stop as soon as it
finds obegin == oend or wait until it knows there's another item to be
copied. The former behaviour saves a little time in the case that no
more items are wanted once the output buffer is full. The latter
behaviour avoids the occasional need for another pass that copies
nothing, if copy_if is called repeatedly to fill an output buffer which
is then flushed.

Ben.


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net