471,612 Members | 2,430 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,612 software developers and data experts.

Copy elements from one STL container to another STL container

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.

Feb 16 '06 #1
4 2520
Marko.Cain...@gmail.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.


You should be able to do that (somewhat inefficiently) with
remove_copy_if. Your predicate would be something like this:

// warning: untested code.
struct predicate {
bool failed;

predicate() : failed(false) {}
bool operator()(X const &x) {
return failed |= !your_real_condition(x);
}
};

I'm not sure whether this is really a good idea though. What you want
doesn't really fit what remove_copy_if is intended to do, so it's
probably better to create a new algorithm that more accurately reflects
what you want.

--
Later,
Jerry.

The universe is a figment of its own imagination.

Feb 16 '06 #2
In article <11**********************@z14g2000cwz.googlegroups .com>,
Ma***********@gmail.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.
Feb 16 '06 #3
do you know any STL algorithm that stops by a condition rather than a
range?

Feb 16 '06 #4
In article <11**********************@g43g2000cwa.googlegroups .com>,
"Diego Martins" <jo********@gmail.com> wrote:
do you know any STL algorithm that stops by a condition rather than a
range?


find_if

--
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.
Feb 16 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Nobody | last post: by
9 posts views Thread by Gunnar G | last post: by
3 posts views Thread by Nudge | last post: by
14 posts views Thread by Jinjun Xu | last post: by
2 posts views Thread by Liming | last post: by
7 posts views Thread by vj | last post: by
2 posts views Thread by John | last post: by
35 posts views Thread by dragoncoder | last post: by
reply views Thread by leo001 | last post: by
1 post views Thread by ZEDKYRIE | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.