"Sean Dettrick" <(E-Mail Removed)> wrote in message

news:(E-Mail Removed) m...

> Hi,

> I have several <vector>'s of the same length, with entries

> as follows:
std::vector<int>'s I assume?

>

> I=[0,3,6,0,5,3,0]

> A=[1,2,3,4,5,6,7]

> B=[2,3,4,5,6,7,8]

>

> I want to use STL to make (I == 0) a mask to operate on the

> elements of A and B, i.e. I want to do this:

>

> for (int i=0;i<I.size();i++){

> if ( I[i] == 0 ){

> A[i] = 0;

> B[i] = 0;

> }

> }

>

> In fortran 90 it would be easy:

> A = merge( 0, A, I==0 )

> B = merge( 0, B, I==0 )

>

> Can anybody point out how to do it in C++? I don't see how

> replace_if can do it.
replace_if could do it with some manipulation: form 2

std::vector<std:

air<int,int> >, one of corresponding elements of A and I,

the other of corresponding elements of B and I, and have your predicate

replace the changed elements with pair<int,in>(0,0), then write the A or B

elements back into their vectors. It really isn't the right algorithm for

this, though. The replace algorithms do the test on the same container

whose elements they change, but you want to change elements of a different

container than the one that has the test values. You'd be better off using

std::transform. The standard library algorithms are rather elemental though

general, so you will not find highly specialized usages like you've written

above for F90. Also, C++ doesn't have expression lambdas like the "I==0"

you wrote; predicates and operators for the standard algorithms are done as

explicit functions or functors. So you need an operator which will do the

test and return either 0 or the original value: a simple function will do:

int trans_op(int orig,int test){ return test?orig:0;}

and then use the binary-operator form of std::transform:

std::transform(A.begin(),A.end(),I.begin(),A.begin (),trans_op);

std::transform(B.begin(),B.end(),I.begin(),B.begin (),trans_op);

-- Hiram Berry