looking for a nicer solution using functionals

Discussion in 'C++' started by persres@googlemail.com, Sep 19, 2011.

  1. Guest

    Hi,
    I need to do the following. I really feel I should be able to
    do it using some existing standard functionals either in boost or stl.

    Does anyone have suggestions.

    template <class T>
    struct maxif : binary_function<int, int, void>
    {
    void operator() (const int&s, int& d) const
    {
    if (d < s)
    d = s;
    }
    };
    void UpdateIfLessThanVal(vector<int> v, const int val)
    {
    for_each(v.begin(), v.end(), bind1st(maxif <int> (), val) );
    }
    , Sep 19, 2011
    #1
    1. Advertising

  2. If I understand you would set a minimum value to all vectors elements? so if you have

    std::vector< int > v = { 1, 2, 3, 4, 5, 6 };
    UpdateIfLessThanVal( v, 4 );

    // now v is { 4, 4, 4, 4, 5, 6 }

    am I right?

    using standard algorithms you could write:

    void UpdateIfLessThanVal(vector<int>& v, const int val)
    {
    std::replace_if( v.begin(), v.end(),
    std::bind2nd( std::less< int >(), val ), val );
    }

    or in C++0x with lambdas (a bit of genericity as a plus)

    template< typename ForwardIterator, typename T >
    void UpdateIfLessThanVal( ForwardIterator first,
    ForwardIterator last,
    const T& val )
    {
    std::replace_if( first, last, [ &val ]( const T& elem )
    {
    return elem < val;
    }, val );
    }

    Hope this help,
    Cheers,
    Fulvio
    Fulvio Esposito, Sep 19, 2011
    #2
    1. Advertising

  3. red floyd Guest

    On 9/19/2011 11:52 AM, Fulvio Esposito wrote:
    > If I understand you would set a minimum value to all vectors elements? so if you have
    >
    > std::vector< int> v = { 1, 2, 3, 4, 5, 6 };
    > UpdateIfLessThanVal( v, 4 );
    >
    > // now v is { 4, 4, 4, 4, 5, 6 }
    >
    > am I right?
    >
    > using standard algorithms you could write:
    >
    > void UpdateIfLessThanVal(vector<int>& v, const int val)
    > {
    > std::replace_if( v.begin(), v.end(),
    > std::bind2nd( std::less< int>(), val ), val );
    > }
    >


    I was going to suggest std::transform, but yours is better.

    And to make things generic:

    template<typename T>
    void UpdateIfLessThanVal(std::vector<T>& v, const T& val)
    {
    std::replace_if(v.begin(), v.end(),
    std::bind2nd(std::less<T>(), val),
    val );
    }
    red floyd, Sep 19, 2011
    #3
  4. Guest

    replace_if is perfect. Thank you.
    , Sep 20, 2011
    #4
    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. dede
    Replies:
    0
    Views:
    393
  2. Michele Simionato

    in search of a nicer super ...

    Michele Simionato, May 18, 2004, in forum: Python
    Replies:
    0
    Views:
    267
    Michele Simionato
    May 18, 2004
  3. Markus Rosenstihl

    Nicer way of strip and replace?

    Markus Rosenstihl, Oct 11, 2005, in forum: Python
    Replies:
    6
    Views:
    362
    Markus Rosenstihl
    Oct 12, 2005
  4. Paul Furman

    nicer url thoughts?

    Paul Furman, Jul 26, 2007, in forum: HTML
    Replies:
    36
    Views:
    1,055
    John Dunlop
    Sep 23, 2007
  5. Vincent Fourmond

    A nicer-looking ri

    Vincent Fourmond, Sep 29, 2006, in forum: Ruby
    Replies:
    5
    Views:
    107
    Eric Hodel
    Oct 2, 2006
Loading...

Share This Page