BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

How to use a list's sort() with my own sort-function, if thi

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Language C++)
View previous topic :: View next topic  
Author Message
Guest






PostPosted: Wed Jul 05, 2006 8:10 am    Post subject: How to use a list's sort() with my own sort-function, if thi Reply with quote



How to use a list's sort() with my own sort-function, if this one is
inside a class?

Inside a class of mine, I want to use STL's sort() function for a list
with my own sort-routine.

When defining it as
int my_sort(struct my_type &s1, struct my_type &s2) {
...
}
outside(!) my class this compiles fine.

Making it a methode of my class returns a compile-error inside
(Dinkum's) list.hpp, #942, translated as

A value of type void is not valid

Unfortunately inside my function I need access to other (private)
members of my class, so it really should be realized as part of the
class.

Is there a solution?

Thanks,

Michael
Back to top
Thorsten Kettner
Guest





PostPosted: Wed Jul 05, 2006 8:10 am    Post subject: Re: How to use a list's sort() with my own sort-function, if Reply with quote



Please see my response in vcl.components.using.
Back to top
Sabetay Toros
Guest





PostPosted: Wed Jul 05, 2006 6:41 pm    Post subject: Re: How to use a list's sort() with my own sort-function, if Reply with quote



MR wrote:
Quote:
How to use a list's sort() with my own sort-function, if this one is
inside a class?

Inside a class of mine, I want to use STL's sort() function for a list
with my own sort-routine.

When defining it as
int my_sort(struct my_type &s1, struct my_type &s2) {
...
}
outside(!) my class this compiles fine.

Making it a methode of my class returns a compile-error inside
(Dinkum's) list.hpp, #942, translated as

A value of type void is not valid

Unfortunately inside my function I need access to other (private)
members of my class, so it really should be realized as part of the
class.

Is there a solution?

Thanks,

Michael
Michael,

Use boost library;
An example

class TFoo
{
public :
int my_sort(const struct my_type &s1, const struct my_type &s2);
};
FList is populated with TFoo objects;

sort(FList.begin(), FList.end(), boost::bind(&TFoo::mysort,
this, _1, _2) );
Back to top
Sabetay Toros
Guest





PostPosted: Wed Jul 05, 2006 6:44 pm    Post subject: Re: How to use a list's sort() with my own sort-function, if Reply with quote

MR wrote:
Quote:
How to use a list's sort() with my own sort-function, if this one is
inside a class?

Inside a class of mine, I want to use STL's sort() function for a list
with my own sort-routine.

When defining it as
int my_sort(struct my_type &s1, struct my_type &s2) {
...
}
outside(!) my class this compiles fine.

Making it a methode of my class returns a compile-error inside
(Dinkum's) list.hpp, #942, translated as

A value of type void is not valid

Unfortunately inside my function I need access to other (private)
members of my class, so it really should be realized as part of the
class.

Is there a solution?

Thanks,

Michael
Michael,

Use boost library;
An example

class TFoo
{
public :

list< my_type > FList;
int my_sort(const struct my_type &s1, const struct my_type &s2);
};

sort(FList.begin(), FList.end(), boost::bind(&TFoo::mysort,
this, _1, _2) );


Sabetay
Back to top
Guest






PostPosted: Sat Jul 08, 2006 7:16 pm    Post subject: Re: How to use a list's sort() with my own sort-function, if Reply with quote

Quote:
Use boost library;

Perhaps I should really think about installing boost...

Thank you,

Michael
Back to top
Old Wolf
Guest





PostPosted: Wed Jul 12, 2006 8:10 am    Post subject: Re: How to use a list's sort() with my own sort-function, if Reply with quote

<MR> wrote:
Quote:
How to use a list's sort() with my own sort-function, if this
one is inside a class?

When defining it as
int my_sort(struct my_type &s1, struct my_type &s2) {
...
}
outside(!) my class this compiles fine.

(Dinkum's) list.hpp, #942, translated as

A value of type void is not valid

Unfortunately inside my function I need access to other
(private) members of my class, so it really should be
realized as part of the class.

Declare the function as static. Then pass &ClassName::my_sort
to the sort function. (There's no way to pass a non-static
member function to the sort function, so I am curious as to
what you wrote to get that Dinkum error message :)

Also it would be good to call it "my_compare" or something
similar, as the purpose of the function is a comparison
(not a sort). It may turn out that the function is useful
for comparisons in other situations.

Make sure that your function is a strict ordering,
otherwise the sort may malfunction:
1) X < Y is always the same (if X and Y are the same)
2) if X < Y and Y < Z, then X < Z
3) if X < Y is true then Y < X is false, and vice versa
4) if X == Y, then X < Y and Y > X are both false.

Usually a good design involves objects being sortable
based on publicly accessible data alone. It's best practice
to have the compare function a non-member, so you may want
to rethink your design a little.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Language C++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.