Copy elements from one STL container to another STL container

Discussion in 'C++' started by Marko.Cain.23@gmail.com, Feb 16, 2006.

  1. Guest

    How can I copy elements from one STL container to another STL container
    if a condition is met and if it find an element fails that condition,
    it stops the copying?

    I can't use the original copy<> algorithm since it does not take a
    binary operation.
    And I don't think I can use transform either since it does not stop if
    the binary operation return false.

    Please correct me if I am wrong or any ideas to my problem.
     
    , Feb 16, 2006
    #1
    1. Advertising

  2. Jerry Coffin Guest

    wrote:
    > How can I copy elements from one STL container to another STL container
    > if a condition is met and if it find an element fails that condition,
    > it stops the copying?
    >
    > I can't use the original copy<> algorithm since it does not take a
    > binary operation.
    > And I don't think I can use transform either since it does not stop if
    > the binary operation return false.


    You should be able to do that (somewhat inefficiently) with
    remove_copy_if. Your predicate would be something like this:

    // warning: untested code.
    struct predicate {
    bool failed;

    predicate() : failed(false) {}
    bool operator()(X const &x) {
    return failed |= !your_real_condition(x);
    }
    };

    I'm not sure whether this is really a good idea though. What you want
    doesn't really fit what remove_copy_if is intended to do, so it's
    probably better to create a new algorithm that more accurately reflects
    what you want.

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
     
    Jerry Coffin, Feb 16, 2006
    #2
    1. Advertising

  3. Daniel T. Guest

    In article <>,
    wrote:

    > How can I copy elements from one STL container to another STL container
    > if a condition is met and if it find an element fails that condition,
    > it stops the copying?
    >
    > I can't use the original copy<> algorithm since it does not take a
    > binary operation.
    > And I don't think I can use transform either since it does not stop if
    > the binary operation return false.
    >
    > Please correct me if I am wrong or any ideas to my problem.


    copy( in.begin(), find_if( in.begin(), in.end(), pred ), out.begin() );

    The above will scan the container 'in' and find the first element that
    satisfies 'pred', then the copy algorithm will copy from the beginning
    of 'in' to the iterator that was found, into 'out'.

    This is a two pass process, if that is unacceptable, you will need to
    make a new algorithm:

    template < typename In, typename Out, typename Pred >
    Out copy_unitl( In first, In last, Out first2, Pred fn ) {
    while ( first != last && !fn( *first ) )
    *first2++ = *first++;
    return first2;
    }


    --
    Magic depends on tradition and belief. It does not welcome observation,
    nor does it profit by experiment. On the other hand, science is based
    on experience; it is open to correction by observation and experiment.
     
    Daniel T., Feb 16, 2006
    #3
  4. do you know any STL algorithm that stops by a condition rather than a
    range?
     
    Diego Martins, Feb 16, 2006
    #4
  5. Daniel T. Guest

    In article <>,
    "Diego Martins" <> wrote:

    > do you know any STL algorithm that stops by a condition rather than a
    > range?


    find_if

    --
    Magic depends on tradition and belief. It does not welcome observation,
    nor does it profit by experiment. On the other hand, science is based
    on experience; it is open to correction by observation and experiment.
     
    Daniel T., Feb 16, 2006
    #5
    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. Wolfgang Lipp
    Replies:
    1
    Views:
    418
    Patrick TJ McPhee
    Jan 30, 2004
  2. Wolfgang Lipp
    Replies:
    0
    Views:
    498
    Wolfgang Lipp
    Jan 28, 2004
  3. Maitre Bart
    Replies:
    2
    Views:
    549
    Maitre Bart
    Feb 11, 2004
  4. Alex
    Replies:
    2
    Views:
    1,289
  5. Hicham Mouline
    Replies:
    1
    Views:
    423
    Kai-Uwe Bux
    Apr 11, 2010
Loading...

Share This Page