STL transform algorithm

Discussion in 'C++' started by Piotr, Feb 14, 2006.

  1. Piotr

    Piotr Guest

    Hi,

    I have the following code which uses STL transform algorithm. It
    basically takes a list of Rect* object, get all the y attribute and
    store it in a vector of 'int'.

    My question is: is there a way to simplify the code? Is there a way for
    me to get rid of the GetY class, since it essentially just calls
    'getY()'. Thank you.

    class GetY : public std::unary_function<Rect*, int>
    {
    public:
    int operator()(Rect* r);
    };

    int GetY :: operator()( Rect* r)
    {
    return r->getY();
    }


    vector<int>& getY(list<Rect*> _rectList) {

    vector<int>* _y = new vector<int>(_rectList.size()),
    transform(_rectList.begin(), _rectList.end(), _y->begin(), GetY());

    return *(_y);
    }
    Piotr, Feb 14, 2006
    #1
    1. Advertising

  2. Piotr

    Guest

    Piotr wrote:
    > Hi,
    >
    > I have the following code which uses STL transform algorithm. It
    > basically takes a list of Rect* object, get all the y attribute and
    > store it in a vector of 'int'.
    >
    > My question is: is there a way to simplify the code? Is there a way for
    > me to get rid of the GetY class, since it essentially just calls
    > 'getY()'. Thank you.
    >
    > class GetY : public std::unary_function<Rect*, int>
    > {
    > public:
    > int operator()(Rect* r);
    > };
    >
    > int GetY :: operator()( Rect* r)
    > {
    > return r->getY();
    > }
    >
    >
    > vector<int>& getY(list<Rect*> _rectList) {
    >
    > vector<int>* _y = new vector<int>(_rectList.size()),
    > transform(_rectList.begin(), _rectList.end(), _y->begin(), GetY());


    You try mem_fun?

    transform(begin, end, mem_fun(&Rect::GetY));
    >
    > return *(_y);
    > }
    , Feb 14, 2006
    #2
    1. Advertising

  3. Piotr

    Piotr Guest

    Is there a way to replace these binary function as well? I use them in
    STL sort algorithm:

    bool GreaterX::eek:perator()( Rect* bd1, Rect* bd2)
    {
    return (bd1->getX() < bd2->getX());
    }


    bool SameX::eek:perator()( Rect* bd1, Rect* bd2)
    {
    return (bd1->getX() == bd2->getX());
    }
    Piotr, Feb 14, 2006
    #3
  4. Piotr

    Guest

    Piotr wrote:
    > Is there a way to replace these binary function as well? I use them in
    > STL sort algorithm:
    >
    > bool GreaterX::eek:perator()( Rect* bd1, Rect* bd2)
    > {
    > return (bd1->getX() < bd2->getX());
    > }
    >
    >
    > bool SameX::eek:perator()( Rect* bd1, Rect* bd2)
    > {
    > return (bd1->getX() == bd2->getX());
    > }


    There is all sorts of useful goodies in <algorithm>. Have a look there
    and/or get Josuttis's book on the std lib.

    I don't know, I would have to look it up.
    , Feb 14, 2006
    #4
  5. Piotr wrote:
    > Hi,
    >
    > I have the following code which uses STL transform algorithm. It
    > basically takes a list of Rect* object, get all the y attribute and
    > store it in a vector of 'int'.
    >
    > My question is: is there a way to simplify the code? Is there a way for
    > me to get rid of the GetY class, since it essentially just calls
    > 'getY()'. Thank you.


    Yes. Take a look at std::mem_fun(), which permits you to eliminate the
    GetY functor entirely. Here's a reference:

    http://www.sgi.com/tech/stl/mem_fun_t.html

    Best regards,

    Tom
    Thomas Tutone, Feb 14, 2006
    #5
  6. Piotr

    Daniel T. Guest

    In article <>,
    "Piotr" <> wrote:

    > Is there a way to replace these binary function as well? I use them in
    > STL sort algorithm:
    >
    > bool GreaterX::eek:perator()( Rect* bd1, Rect* bd2)
    > {
    > return (bd1->getX() < bd2->getX());
    > }
    >
    >
    > bool SameX::eek:perator()( Rect* bd1, Rect* bd2)
    > {
    > return (bd1->getX() == bd2->getX());
    > }


    (I'm assuming getX returns an int.)

    In both cases, the functions take two Rect*s, call "->getX()" on each
    and performs a compare on the results returning a bool. The only
    difference is what is being used to make the comparison.

    It just so happens that there are two functors in the standard library
    that already do the two different comparisons (std::less and
    std::equal_to) so we simply need to create one class that can use either
    of those functors. It's operator() will look something like this:

    bool operator()( const Rect* bd1, const Rect* bd2 ) const {
    return fn( bd1->getX(), bd2->getX() );
    }

    If 'fn' is std::less, then it will return true if bd1's x is less than
    bd2's x. If 'fn' is std::equal_to, then the two xs must be equal for it
    to return true.

    Here is the whole class.

    template <typename Op>
    class compare_x_t: public binary_function<Rect*, Rect*, bool>
    {
    Op fn;
    public:
    compare_x_t() { }
    compare_x_t( Op f ): fn( f ) { }
    bool operator()( const Rect* bd1, const Rect* bd2 ) const {
    return fn( bd1->getX(), bd2->getX() );
    }
    };

    and a helper function for creating the right class object.

    template <typename Op>
    compare_x_t<Op> compare_x( Op f ) {
    return compare_x_t<Op>( f );
    }


    Now you can:

    void fn( list<Rect*>& ll, Rect* refRect ) {
    ll.sort( compare_x( less<int>() ) );

    list<Rect*>::iterator it = find_if( ll.begin(), ll.end(),
    bind2nd( compare_x( equal_to<int>() ), refRect ) );
    }

    The above could be made more generic but don't do it unless you need it.

    --
    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 14, 2006
    #6
    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. Steffen Brinkmann

    <algorithm> transform modification

    Steffen Brinkmann, Sep 21, 2003, in forum: C++
    Replies:
    20
    Views:
    765
    Steffen Brinkmann
    Sep 24, 2003
  2. Patrick Guio

    STL transform algorithm

    Patrick Guio, Nov 2, 2005, in forum: C++
    Replies:
    9
    Views:
    529
    Clark S. Cox III
    Nov 2, 2005
  3. Diego Martins

    Transform algorithm 1-n, n-1 or m-n

    Diego Martins, Oct 18, 2006, in forum: C++
    Replies:
    2
    Views:
    286
    Fraser Ross
    Oct 19, 2006
  4. suresh
    Replies:
    2
    Views:
    318
    suresh
    Dec 30, 2007
  5. Gerald I. Evenden

    Problem with <algorithm> transform

    Gerald I. Evenden, Jan 6, 2008, in forum: C++
    Replies:
    13
    Views:
    544
    James Kanze
    Jan 8, 2008
Loading...

Share This Page