Reply embedded...

> -----Original Message-----
> From: [email protected] [mailto:[EMAIL PROTECTED] On Behalf Of
> rgearyiii
> Sent: Wednesday, November 29, 2006 10:00 PM
> To: [email protected]
> Subject: [c-prog] Re: STL sort help
> 
> --- In [email protected], "Shyan Lam" <[EMAIL PROTECTED]> wrote:
> >
> > Because the predicate should not be a non-static member.
> 
> Since I need the predicate to access member data of the instance of the
> class from which it is called, I'm still stuck then.

No you don't, at least it is not shown in your example code.  


> Is there an easy modification to the STL althorithm that would allow me
> to use a non-static predicate?

There is no need to, what passed to the predicate are two elements.  Even if
the elements are class, you call the class function members to access the
class's data members.

#include <algorithm>

class MY_CLASS
{
public:
    MY_CLASS(const int i1, const int i2) : i1(i1), i2(i2) {}

    int get_i1(void) const { return i1; }
    int get_i2(void) const { return i2; }

private:
    int i1;
    int i2;
};

bool MyCompare(const MY_CLASS& c1, const MY_CLASS& c2)
{
    return (c1.get_i1() + c1.get_i2()  < c2.get_i1() + c2.get_i2());
}


int main()
{
    MY_CLASS myC[] = 
    {
        MY_CLASS(10, 5), 
        MY_CLASS( 8, 6), 
        MY_CLASS( 6, 7),
        MY_CLASS( 4, 8),
        MY_CLASS( 2, 9)
    };

    std::sort(&myC[0], &myC[5], MyCompare);

    return 0;
}

Show an example of what you think you need.

HTH
Shyan



Reply via email to