c++ <vector> version of fortran "merge" or "where"

S

Sean Dettrick

Hi,
I have several <vector>'s of the same length, with entries
as follows:

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 == 0 ){
A = 0;
B = 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.

Thanks in advance,
Sean Dettrick
 
H

Hiram Berry

Sean Dettrick said:
Hi,
I have several <vector>'s of the same length, with entries
as follows:

std::vector said:
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 == 0 ){
A = 0;
B = 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::pair<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
 

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
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top