469,628 Members | 1,192 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,628 developers. It's quick & easy.

std::sort using a member function as the BinaryPredicate

Does anyone know how to do this?
Sep 8 '05 #1
15 3111
"Peter Olcott" <ol****@att.net> wrote in news:xQNTe.8191$UI.4718
@okepread05:
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)

Sep 8 '05 #2

"Andre Kostur" <nn******@kostur.net> wrote in message news:Xn*******************************@207.35.177. 134...
"Peter Olcott" <ol****@att.net> wrote in news:xQNTe.8191$UI.4718
@okepread05:
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)

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.
Sep 8 '05 #3
"Peter Olcott" <ol****@att.net> wrote in
news:ifOTe.8193$UI.2941@okepread05:

"Andre Kostur" <nn******@kostur.net> wrote in message
news:Xn*******************************@207.35.177. 134...
"Peter Olcott" <ol****@att.net> wrote in news:xQNTe.8191$UI.4718
@okepread05:
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)

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?
Sep 8 '05 #4

"Andre Kostur" <nn******@kostur.net> wrote in message news:Xn*******************************@207.35.177. 134...
"Peter Olcott" <ol****@att.net> wrote in
news:ifOTe.8193$UI.2941@okepread05:

"Andre Kostur" <nn******@kostur.net> wrote in message
news:Xn*******************************@207.35.177. 134...
"Peter Olcott" <ol****@att.net> wrote in news:xQNTe.8191$UI.4718
@okepread05:

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)

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?


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
Sep 8 '05 #5
"Peter Olcott" <ol****@att.net> wrote in
news:oJOTe.8194$UI.775@okepread05:

"Andre Kostur" <nn******@kostur.net> wrote in message
news:Xn*******************************@207.35.177. 134...
"Peter Olcott" <ol****@att.net> wrote in
news:ifOTe.8193$UI.2941@okepread05:

"Andre Kostur" <nn******@kostur.net> wrote in message
news:Xn*******************************@207.35.177. 134...
"Peter Olcott" <ol****@att.net> wrote in news:xQNTe.8191$UI.4718
@okepread05:

> 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)

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?


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?
Sep 8 '05 #6

Peter Olcott wrote:
Does anyone know how to do this?


http://boost.org/libs/bind/bind.html

Sep 8 '05 #7

"Andre Kostur" <nn******@kostur.net> wrote in message news:Xn*******************************@207.35.177. 134...
"Peter Olcott" <ol****@att.net> wrote in
news:oJOTe.8194$UI.775@okepread05:

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?


std::sort is invoked from another member function.
Sep 8 '05 #8
"Peter Olcott" <ol****@att.net> wrote in
news:G1XTe.8197$UI.2185@okepread05:

"Andre Kostur" <nn******@kostur.net> wrote in message
news:Xn*******************************@207.35.177. 134...
"Peter Olcott" <ol****@att.net> wrote in
news:oJOTe.8194$UI.775@okepread05:

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?


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?
Sep 8 '05 #9

"Andre Kostur" <nn******@kostur.net> wrote in message news:Xn*******************************@207.35.177. 135...
"Peter Olcott" <ol****@att.net> wrote in
news:G1XTe.8197$UI.2185@okepread05:

"Andre Kostur" <nn******@kostur.net> wrote in message
news:Xn*******************************@207.35.177. 134...
"Peter Olcott" <ol****@att.net> wrote in
news:oJOTe.8194$UI.775@okepread05:

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?


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?


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.
}

Sep 8 '05 #10
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 ));

"Peter Olcott" <ol****@att.net> schrieb im Newsbeitrag
news:xQNTe.8191$UI.4718@okepread05...
Does anyone know how to do this?

Sep 8 '05 #11
"Peter Olcott" <ol****@att.net> wrote in
news:5WXTe.8200$UI.3748@okepread05:

"Andre Kostur" <nn******@kostur.net> wrote in message
news:Xn*******************************@207.35.177. 135...
"Peter Olcott" <ol****@att.net> wrote in
news:G1XTe.8197$UI.2185@okepread05:

"Andre Kostur" <nn******@kostur.net> wrote in message
news:Xn*******************************@207.35.177. 134...
"Peter Olcott" <ol****@att.net> wrote in
news:oJOTe.8194$UI.775@okepread05:

> 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?

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?


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....
Sep 8 '05 #12

"Andre Kostur" <nn******@kostur.net> wrote in message news:Xn*******************************@207.35.177. 135...
"Peter Olcott" <ol****@att.net> wrote in
news:5WXTe.8200$UI.3748@okepread05:

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....


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.
Sep 8 '05 #13
"Oliver (Nospam)" <ok*****@arcor.de> wrote in message news:43**********************@newsread2.arcor-online.net...
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 ));

"Peter Olcott" <ol****@att.net> schrieb im Newsbeitrag
news:xQNTe.8191$UI.4718@okepread05...
Does anyone know how to do this?


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.
}

Sep 8 '05 #14
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" <ol****@att.net> schrieb im Newsbeitrag
news:8a_Te.8207$UI.3814@okepread05...
"Oliver (Nospam)" <ok*****@arcor.de> wrote in message

news:43**********************@newsread2.arcor-online.net...
.... (removed, oliver)
Sep 9 '05 #15

"Oliver (Nospam)" <ok*****@arcor.de> wrote in message news:43**********************@newsread2.arcor-online.net...
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
Sep 9 '05 #16

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by lok | last post: by
9 posts views Thread by JasBascom | last post: by
7 posts views Thread by Ireneusz SZCZESNIAK | last post: by
8 posts views Thread by Manfred | last post: by
5 posts views Thread by fade | last post: by
1 post views Thread by panzhiyong | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.