std::sort using a member function as the BinaryPredicate

A

Andre Kostur

Does anyone know how to do this?

A member function of what? Your description is somewhat vague. A member
function of some other unrelated class. For example:

std::sort(somevector.begin(), somevector.end(), &otherclass::memfun);

? (and yes, I know the above won't work... but serves for illustration
purposes)
 
P

Peter Olcott

Andre Kostur said:
A member function of what? Your description is somewhat vague. A member
function of some other unrelated class. For example:

std::sort(somevector.begin(), somevector.end(), &otherclass::memfun);

? (and yes, I know the above won't work... but serves for illustration
purposes)
What I mean is as opposed to a global function or a static member function.
std::sort(RandomAccessIterator beg, RandomAccessIterator end, BinaryPredicate op)

I want to somehow adapt a member function so that it can be
used as the BinaryPredicate parameter.
 
A

Andre Kostur

What I mean is as opposed to a global function or a static member
function. std::sort(RandomAccessIterator beg, RandomAccessIterator
end, BinaryPredicate op)

I want to somehow adapt a member function so that it can be
used as the BinaryPredicate parameter.

Yes... a member of _what_ class? Some other unrelated class? Whatever
class *beg refers to?
 
P

Peter Olcott

Andre Kostur said:
Yes... a member of _what_ class? Some other unrelated class? Whatever
class *beg refers to?

any arbitrary class. I am only looking for the exact syntax
to convert a binary member function into a binary global function.
Something like mem_fun_ref
 
A

Andre Kostur

any arbitrary class. I am only looking for the exact syntax
to convert a binary member function into a binary global function.
Something like mem_fun_ref

Ummm.. you've got a problem though. Member functions are invoked on an
object. Where's std::sort going to get an instance of the object upon
which to invoke the mem_fun_ref?
 
P

Peter Olcott

Andre Kostur said:
Ummm.. you've got a problem though. Member functions are invoked on an
object. Where's std::sort going to get an instance of the object upon
which to invoke the mem_fun_ref?

std::sort is invoked from another member function.
 
A

Andre Kostur

std::sort is invoked from another member function.

That's fine, but inside std::sort, how is it going to get a
pointer/reference to anything else (except of course the two iterators and
the BinaryPredicate)? When you call a global function (from within another
member function), how do you know from which object instance that global
function was called from?
 
P

Peter Olcott

Andre Kostur said:
That's fine, but inside std::sort, how is it going to get a
pointer/reference to anything else (except of course the two iterators and
the BinaryPredicate)? When you call a global function (from within another
member function), how do you know from which object instance that global
function was called from?

class Arbitrary {
std::vector<AnythingAtAll> Anything;
bool LessThan(const AnythingAtAll& Any1, const AnythingAtAll& Any2);
//
// The next line will not compile because LessThan is a member function
// I need the name of the Function Adaptor that converts this into the
// form required by std::sort. I don't know its name.
//
void sort(){ std::sort(Anything.begin(), Anything.end(), LessThan); };
}

bool LessThan(const AnythingAtAll& Any1, const AnythingAtAll& Any2) {
// return Any1 < Any2
// This MUST be a Member Function because it requires access
// to additional member data besides the std::vector data.
}
 
G

Guest

1. Approach (Member function has arbitrary name)
template <typename T>
struct helper: binary_function<T,T,bool>
{
T p;
helper(T p0): p(p0) {}
bool operator( T a, T b ) { return p->Member(a, b); }
}

T* myClass = new T;
sort(begin(),end(),helper(myClass));

2. Approach (Member function can be written as "bool operator(T a, T b)")
class T
{
bool member(T a, T b) {...};
bool operator(T a, T b) { return member(a,b); };
}
T* myClass = new T;
sort(begin(),end(),(*myClass ));
 
A

Andre Kostur

class Arbitrary {
std::vector<AnythingAtAll> Anything;
bool LessThan(const AnythingAtAll& Any1, const AnythingAtAll& Any2);
//
// The next line will not compile because LessThan is a member
function // I need the name of the Function Adaptor that converts
this into the // form required by std::sort. I don't know its name.
//
void sort(){ std::sort(Anything.begin(), Anything.end(), LessThan);
};
}

bool LessThan(const AnythingAtAll& Any1, const AnythingAtAll& Any2) {
// return Any1 < Any2
// This MUST be a Member Function because it requires access
// to additional member data besides the std::vector data.
}

OK, you'll need a helper class. See the post by Oliver... but correct it
to use pointers and references where appropriate (like the T a; member
variable should probably be T& a;, and the parameters to operator()
should be const-ref... that sort of thing), otherwise you'll probably end
up making unnecessary copies of stuff....
 
P

Peter Olcott

Andre Kostur said:
OK, you'll need a helper class. See the post by Oliver... but correct it
to use pointers and references where appropriate (like the T a; member
variable should probably be T& a;, and the parameters to operator()
should be const-ref... that sort of thing), otherwise you'll probably end
up making unnecessary copies of stuff....

I don't have enough knowledge to know what would need to be
corrected. The best that I could do is research this basic idea,
and proceed by trial-and-error.
 
P

Peter Olcott

Oliver (Nospam) said:
1. Approach (Member function has arbitrary name)
template <typename T>
struct helper: binary_function<T,T,bool>
{
T p;
helper(T p0): p(p0) {}
bool operator( T a, T b ) { return p->Member(a, b); }
}

T* myClass = new T;
sort(begin(),end(),helper(myClass));

2. Approach (Member function can be written as "bool operator(T a, T b)")
class T
{
bool member(T a, T b) {...};
bool operator(T a, T b) { return member(a,b); };
}
T* myClass = new T;
sort(begin(),end(),(*myClass ));

The above code is exactly the sort of thing that I am looking for.
Andre Kostur (in his Sept 8, 2005 11:21 AM post) said that there
were some errors in the above code. Could you please double check it?

Below is an example of exactly the sort of code that I need to apply
your idea to. It would be very helpful if you could provide the exact
syntax applying your idea to the code below. Thanks for your help.

class Arbitrary {
std::vector<AnythingAtAll> Anything;
bool LessThan(const AnythingAtAll& Any1, const AnythingAtAll& Any2);
//
// The next line will not compile because LessThan is a member function
// I need the name of the Function Adaptor that converts this into the
// form required by std::sort. I don't know its name.
//
void sort(){ std::sort(Anything.begin(), Anything.end(), LessThan); };
}

bool LessThan(const AnythingAtAll& Any1, const AnythingAtAll& Any2) {
// return Any1 < Any2
// This MUST be a Member Function because it requires access
// to additional member data besides the std::vector data.
}
 
G

Guest

Yeah, I can help you. Let me first explain, my first approach just was
writing down the basic idea without any syntactical thoughts behind. Now
that I know, that's the thing you need, let us hav a look on further
details:

A compact version looks like this:

class Arbitrary {
std::vector<AnythingAtAll> Anything;
bool LessThan(const AnythingAtAll& Any1, const AnythingAtAll& Any2);
bool operator()(const AnythingAtAll& Any1, const AnythingAtAll& Any2) {
return LessThan(Any1, Any2); }
void sort(){ std::sort(Anything.begin(), Anything.end(),(*this)); };
}

Inside std::sort the Pred object will be called as what is called a functor,
ie the call looks like " if ( Pred(*it1, *it2) ...", so what you need is
exactly the operator()( ... ) in your Pred object.
1. The pred object above is now your class Arbitrary. To have a more
flexible solution you can use any extra helper class. This is because you
can have only 1 operator with a single signature.
2. The compact version above can be more compact, if you omit LessThan and
copy the body of it the body of the operator().

If one had understood STL, it's so easy to use. But to have a look behind as
a beginner, can be a rather frustrating job.
If you have more questions, please let me know.

greetings, oliver

Peter Olcott said:
"Oliver (Nospam)" <[email protected]> wrote in message
.... (removed, oliver)
 
P

Peter Olcott

Oliver (Nospam) said:
Yeah, I can help you. Let me first explain, my first approach just was
writing down the basic idea without any syntactical thoughts behind. Now
that I know, that's the thing you need, let us hav a look on further
details:

A compact version looks like this:

class Arbitrary {
std::vector<AnythingAtAll> Anything;
bool LessThan(const AnythingAtAll& Any1, const AnythingAtAll& Any2);
bool operator()(const AnythingAtAll& Any1, const AnythingAtAll& Any2) {
return LessThan(Any1, Any2); }
void sort(){ std::sort(Anything.begin(), Anything.end(),(*this)); };
}

This seems to be a complete answer to my question.
My code does compile now. I am estimating that it
will execute properly. If you don't here from me on this
thread again, it means that the above answer was the
complete answer.

Thanks again for all your help,

Peter Olcott
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top