Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > c++ <vector> version of fortran "merge" or "where"

Reply
Thread Tools

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

 
 
Sean Dettrick
Guest
Posts: n/a
 
      06-27-2003
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[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.

Thanks in advance,
Sean Dettrick
 
Reply With Quote
 
 
 
 
Hiram Berry
Guest
Posts: n/a
 
      06-28-2003
"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


 
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
Passing an array from FORTRAN to C then passing it within C andReturning it to FORTRAN deadpickle C Programming 1 11-07-2010 02:38 PM
Re: Where to get stand alone Dot Net Framework version 1.1, version2.0, version 3.0, version 3.5, version 2.0 SP1, version 3.0 SP1 ? MowGreen [MVP] ASP .Net 5 02-09-2008 01:55 AM
Re: Where to get stand alone Dot Net Framework version 1.1, version 2.0, version 3.0, version 3.5, version 2.0 SP1, version 3.0 SP1 ? PA Bear [MS MVP] ASP .Net 0 02-05-2008 03:28 AM
Re: Where to get stand alone Dot Net Framework version 1.1, version 2.0, version 3.0, version 3.5, version 2.0 SP1, version 3.0 SP1 ? V Green ASP .Net 0 02-05-2008 02:45 AM
Many Fortran code! How to call Fortran code from C/C++? Luna Moon C++ 9 09-04-2007 03:49 PM



Advertisments