473,395 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

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
Jul 19 '05 #1
1 3884
"Sean Dettrick" <sd*******@hotmail.com> wrote in message
news:c2*************************@posting.google.co 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::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
Jul 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Markus Faust | last post by:
Hi, I'm trying to link Fortran files generated with “Compaq Visual Fortran Optimizing Compiler Version 6.6 (Update B)” under “Enthought Edition build 1028, Python 2.3 (#46, Aug 11 2003,...
15
by: Nick Coghlan | last post by:
Thought some folks here might find this one interesting. No great revelations, just a fairly sensible piece on writing readable code :) The whole article:...
5
by: NM | last post by:
Hi all I am trying to link C++ and Fortran.I am not very familiar with Fortran. I have been successful in linking C++ and fortran using g++ and ifc for simple program. So adding _ to at the end of...
5
by: NM | last post by:
Hi All, I am having a peculiar problem. I used to link between intel fortran 7.0 and g++ without any problem. Now the intel compiler is upgraded to version 9.0 and I am getting segmenatation...
13
by: NM | last post by:
Sometimes ago I was having a problem in linking between C++ and Fortran program. That was solved (using input from this newsgroup) using the Fortran keyword "sequence" with the derived types (to...
26
by: sam | last post by:
Hi, Can anyone help me find a software that can convert a code in 'C' to 'Fortran77/90' automatically? Thanks in advance. Sam.
9
by: travisperkins03 | last post by:
Hi, I have read somewhere that C code sometimes cannot be compiled to be as efficient as FORTRAN, eg for matrix multiplication, because a C compiler cannot make the assumptions about arrays that...
10
by: Julian | last post by:
I get the following error when i try to link a fortran library to a c++ code in .NET 2005. LINK : fatal error LNK1104: cannot open file 'libc.lib' the code was working fine when built using...
52
by: Nomad.C | last post by:
Hi I've been thinking of learning Fortran as number crunching kinda language for my Physics degree......but then looking around the internet, people are saying that the libraries/ Algorithms once...
9
by: a-lbi | last post by:
I use gcc compiler (version 2.8.1). During linking I get the following error message: Undefined first referenced symbol in file log10l ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.