473,785 Members | 3,134 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3911
"Sean Dettrick" <sd*******@hotm ail.com> wrote in message
news:c2******** *************** **@posting.goog le.com...
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_o p);
std::transform( B.begin(),B.end (),I.begin(),B. begin(),trans_o p);

-- Hiram Berry
Jul 19 '05 #2

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

Similar topics

2
5313
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, 09:34:05) on win32” on Windows-XP”. I changed mingw32_support.py as proposed by Pearu Peterson on April 8 2003 (I changed `if 1:` block into `if 0:` block). When trying the following Python session:
15
2103
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: http://www.acmqueue.com/modules.php?name=Content&pa=showpage&pid=271&page=1 The section specifically on white space: http://www.acmqueue.com/modules.php?name=Content&pa=showpage&pid=271&page=3 Cheers,
5
4023
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 fortran subroutine names when calling from C++ is working. Now I want to move to bigger program. I have a rather big Fortran code which compiles and links fine under ifc. Now I changed the program main in Fortran into a subroutine main_sub so...
5
2856
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 fault. While debugging I found when data structures are allocated in C++ and passed to fortran subroutines the fortran subroutines are accessing a different location in the address space. For example I have created a very simple program that shows the...
13
2544
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 assume contiguous space). Now I am having problem again. In order to show the problem I have created small program and this time there is no data straucture being passed between C++ and Fortran. Here is how the program looks like
26
13572
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
3829
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 a FORTRAN compiler can. But I don't understand the example, not least because I don't understand FORTRAN. I also don't understand why it is more efficient in this case for a compiler to choose the order of evaluation (or whatever it is that it...
10
2693
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 .NET2003. also, when I do not try to link the fortran library (just to see if that was the cause), it builds the exe without any problems. i don't even know how to begin addressing this problem...any help would be
52
5134
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 used for number crunching is now slowly converting into C, so do you think I should stick with C, since I know C already, or should I proceed learning fortran?? Any advice?? Thanks
9
3699
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 /opt/gcc_4.1.2/solaris10/lib/ libgfortran.a(write.o) strtof /opt/gcc_4.1.2/solaris10/lib/ libgfortran.a(read.o) strtold /opt/gcc_4.1.2/solaris10/lib/
0
9643
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10147
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9947
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8971
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7496
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4046
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.