In article <11************ **********@z14g 2000cwz.googleg roups.com>,
Ma***********@g mail.com wrote:
How can I copy elements from one STL container to another STL container
if a condition is met and if it find an element fails that condition,
it stops the copying?
I can't use the original copy<> algorithm since it does not take a
binary operation.
And I don't think I can use transform either since it does not stop if
the binary operation return false.
Please correct me if I am wrong or any ideas to my problem.
copy( in.begin(), find_if( in.begin(), in.end(), pred ), out.begin() );
The above will scan the container 'in' and find the first element that
satisfies 'pred', then the copy algorithm will copy from the beginning
of 'in' to the iterator that was found, into 'out'.
This is a two pass process, if that is unacceptable, you will need to
make a new algorithm:
template < typename In, typename Out, typename Pred >
Out copy_unitl( In first, In last, Out first2, Pred fn ) {
while ( first != last && !fn( *first ) )
*first2++ = *first++;
return first2;
}
--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.