473,386 Members | 1,726 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

std::sort using a member function as the BinaryPredicate

Does anyone know how to do this?
Sep 8 '05 #1
15 3301
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: lok | last post by:
i have a class: template <class T1, class T2> class CPairMapping { public: typedef std::pair<T1, T2> ValuePair_t; typedef std::vector<ValuePair_t> ValueList_t; typedef std::binary_function<...
9
by: JasBascom | last post by:
say i had 97456 and 23456 is there already a sort function to check which one begins with the smaller number rearrange it. in this case the bottom number should clearly be rearranged to the...
4
by: hall | last post by:
I accidently overloaded a static member function that I use as predicate in the std::sort() for a vector and ended up with a compiler error. Is this kind of overload not allowed for predicates and...
7
by: Ireneusz SZCZESNIAK | last post by:
I want to sort a vector with the std::sort function. There are two functions: one with two arguments, the other with three arguments. I am using the one with three arguments. I noticed that...
8
by: Manfred | last post by:
Hello I am new to template programming, so i tried the 'example' from http://www.sgi.com/tech/stl/functors.html. I can compile the code but when i want to run the program I get a segmentation...
4
by: prakashsahni | last post by:
I am using a sort func object like struct mystruct { bool operator () (MyClass* const &a, MyClass* const&b) {}; } Invoke it like std::sort(vec.begin(), vec.end(),mystruct); Where vec is an...
5
by: fade | last post by:
Good afternoon, I need some advice on the following: I've got a class that has a member std::vector<CStringm_vFileName and a member CString m_path; The vector contains a bunch of filenames with...
1
by: panzhiyong | last post by:
Hi there! I wonder why there is no such a overload version of sort function in STL: template<class RandomAccessIterator, class Pr, class IterSwap> void sort( RandomAccessIterator _First,...
1
by: Markus Dehmann | last post by:
In the following code example, I define several Comparator classes which contain different compare functions to use with std::sort. I have a Sorter class that gets passed a Comparator and is...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.