Why no copy_if?

R

red floyd

Josuttis comments that to get copy_if functionality, one should use
remove_copy_if. But that removes the copied items from the original container.

Yes, copy_if is trivial to implement, but why did the committee (and/or
Stepanov&Lee) not put it into the STL?
 
S

Sam Holden

Josuttis comments that to get copy_if functionality, one should use
remove_copy_if. But that removes the copied items from the original container.

No it doesn't. It copies items from one range to another, skipping those
that match the condition. The source range is not modified.

*None* of the STL algorithms remove elements from containers. Since they work
on arrays, and it is impossible to change the length of an array in C++.
Yes, copy_if is trivial to implement, but why did the committee (and/or
Stepanov&Lee) not put it into the STL?

It was an oversight, for which Stroustrup apologises in "The C++ Programming
Language".

But remove_copy_if works just fine, you just have to negate the test.
 
S

Stephen Howe

Yes, copy_if is trivial to implement, but why did the committee (and/or
Stepanov&Lee) not put it into the STL?

A mistake. It will be in the next version.

Stephen Howe
 
J

Jerry Coffin

Josuttis comments that to get copy_if functionality, one should use
remove_copy_if. But that removes the copied items from the original container.

No, it does not. It simply skips copying any item that meets the
specified criteria. IOW, copy_if would be basically identical to
remove_copy_if, except that the predicate is logically negated.
 
R

red floyd

Jerry said:
No, it does not. It simply skips copying any item that meets the
specified criteria. IOW, copy_if would be basically identical to
remove_copy_if, except that the predicate is logically negated.

I was looking at Josuttis, page 380:

"remove_copy_if() is a combination of copy() and remove_if(). It *removes each element in the source range*
[beg,end) for which the unary predicate op(elem) yields true while the elements are copied into the destination range
starting with destBeg". (emphasis mine)

The wording was such that I thought they were removed from the source container. However, looking at the sample
program and its output, I see that I was mistaken.

Thanks
 
M

Mike Wahler

Jerry Coffin said:
[ ... ]
I was looking at Josuttis, page 380:

Yup -- a poor explanation. One of the few flaws in an excellent book.

From http://www.josuttis.com/libbook/errata1_5.html :

=====================================================================
Page 380, Section 9.7.1

The descriptions of remove_copy() and remove_copy_if() are misleading.
Replace the second sentence of the first item by:

It copies each element in the source range [beg,end) that is not equal to
value into the destination range starting with destBeg.

Replace the second sentence of the second item by:

It copies each element in the source range [beg,end) for which the unary
predicate op(elem) yields false into the destination range starting with
destBeg.
=====================================================================

-Mike
 
R

red floyd

Mike said:
[ ... ]

I was looking at Josuttis, page 380:

Yup -- a poor explanation. One of the few flaws in an excellent book.


From http://www.josuttis.com/libbook/errata1_5.html :

=====================================================================
Page 380, Section 9.7.1

The descriptions of remove_copy() and remove_copy_if() are misleading.
Replace the second sentence of the first item by:

It copies each element in the source range [beg,end) that is not equal to
value into the destination range starting with destBeg.

Replace the second sentence of the second item by:

It copies each element in the source range [beg,end) for which the unary
predicate op(elem) yields false into the destination range starting with
destBeg.
=====================================================================

Thanks! I needed that!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,826
Members
47,371
Latest member
Brkaa

Latest Threads

Top