Boost logo

Boost Users :

From: Mr Zenaan Harkness (zen_at_[hidden])
Date: 2003-09-13 06:41:30


On Sat, 2003-09-13 at 20:52, Witz 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.
>
> Witz

That's very useful. I've been wondering about that - backinserter is
fine if you really do want to append, but I'd never before seen mention
of a solution for limiting based on size of output.

I guess a final option would be "copy into, then append if full".
Although if you don't fill the container, then presumably there are some
elements still at the end - I can't imagine wanting this. What would
make more sense is "truncate container and then append" - which is
probably better done outside the algorithm, yes?

Thanks heaps - particularly that link is excellent,
Zenaan


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