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

Discussion in 'C++' started by Sean Dettrick, Jun 27, 2003.

  1. 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
     
    Sean Dettrick, Jun 27, 2003
    #1
    1. Advertising

  2. Sean Dettrick

    Hiram Berry Guest

    "Sean Dettrick" <> wrote in message
    news:...
    > 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 == 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
     
    Hiram Berry, Jun 28, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    8
    Views:
    1,961
    Csaba
    Feb 18, 2006
  2. Luna Moon
    Replies:
    9
    Views:
    623
    Guest
    Sep 4, 2007
  3. V Green
    Replies:
    0
    Views:
    885
    V Green
    Feb 5, 2008
  4. PA Bear [MS MVP]
    Replies:
    0
    Views:
    1,000
    PA Bear [MS MVP]
    Feb 5, 2008
  5. deadpickle
    Replies:
    1
    Views:
    1,025
    Jens Thoms Toerring
    Nov 7, 2010
Loading...

Share This Page