Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Copy elements from one STL container to another STL container

Reply
Thread Tools

Copy elements from one STL container to another STL container

 
 
Marko.Cain.23@gmail.com
Guest
Posts: n/a
 
      02-16-2006
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.

 
Reply With Quote
 
 
 
 
Jerry Coffin
Guest
Posts: n/a
 
      02-16-2006
(E-Mail Removed) 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.

 
Reply With Quote
 
 
 
 
Daniel T.
Guest
Posts: n/a
 
      02-16-2006
In article <(E-Mail Removed) .com>,
http://www.velocityreviews.com/forums/(E-Mail Removed) 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.
 
Reply With Quote
 
Diego Martins
Guest
Posts: n/a
 
      02-16-2006
do you know any STL algorithm that stops by a condition rather than a
range?

 
Reply With Quote
 
Daniel T.
Guest
Posts: n/a
 
      02-16-2006
In article <(E-Mail Removed) .com>,
"Diego Martins" <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
number of distinct elements in a const container and iterating over the distinct elements Hicham Mouline C++ 1 04-11-2010 10:56 AM
Deep copy of XML elements from one document to another pesachzon Python 1 06-09-2006 07:39 PM
STL: container's values setup by another container Maitre Bart C++ 2 02-11-2004 12:11 AM
container elements for repeating elements ('element farms') needed? Wolfgang Lipp XML 1 01-30-2004 04:09 PM
container elements for repeating elements ('element farms') needed? Wolfgang Lipp XML 0 01-28-2004 02:50 PM



Advertisments