sorting std::list with function predicate

M

matthias_k

Hi,

I need to sort elements of a std::list using a function predicate,
something like:

bool predicate( const& M m1, const& M m2 ) {
return m1.somedata < m2.somedata;
}

I tried to call std::list::sort with such a predicate but that doesn't work:

mylist.sort( predicate ); // error, no matching function call

So since I must not call std::sort on a list, how can I sort it in a way
other than the standard operator< of its element does?
I need to do it with predicates, because I need to sort the list with
different sorting criterions, depending on the situation.

Regards,
Matthias
 
M

matthias_k

matthias_k said:
Hi,

I need to sort elements of a std::list using a function predicate,
something like:

bool predicate( const& M m1, const& M m2 ) {
return m1.somedata < m2.somedata;
}

I tried to call std::list::sort with such a predicate but that doesn't
work:

mylist.sort( predicate ); // error, no matching function call

So since I must not call std::sort on a list, how can I sort it in a way
other than the standard operator< of its element does?
I need to do it with predicates, because I need to sort the list with
different sorting criterions, depending on the situation.

Regards,
Matthias

Oh yeah. I need to use a functor. Ignore this thread, sorry.
The solution is:

class Predicate
{
public:
bool operator() (const M& lhs, const M& rhs) {
return lhs.somedata < rhs.somedata;
}
};

// ...
mylist.sort( Predicate() );

Regards,
Matthias
 
K

Karthik Kumar

matthias_k said:
Hi,

I need to sort elements of a std::list using a function predicate,
something like:

bool predicate( const& M m1, const& M m2 ) {


This would not compile.

Did you mean to write this ?


"bool predicate( const M & m1, const M & m2 ) "
return m1.somedata < m2.somedata;
}

I tried to call std::list::sort with such a predicate but that doesn't
work:

mylist.sort( predicate ); // error, no matching function call

If the predicate signature is corrected as mentioned above this should
work.
 
M

matthias_k

Karthik said:
This would not compile.

Did you mean to write this ?


"bool predicate( const M & m1, const M & m2 ) "



If the predicate signature is corrected as mentioned above this should
work.

Ugh, yeah sorry... Of course I have that right in the original code, But
that is not the problem. A function predicate just doesn't ssem to work
on std::list::sort. A functor does.

Regards,
Matthias
 
M

matthias_k

Karthik said:
This would not compile.

Did you mean to write this ?


"bool predicate( const M & m1, const M & m2 ) "



If the predicate signature is corrected as mentioned above this should
work.

I just noticed that the function I had written was declared inline.
Maybe this is the reason it didn't work? Can you pass inline functions
to functions which take pointers to functions at all?
 
K

Karthik Kumar

Did you have the function 'predicate' as a member function
in your class or as a separate function ? If the function had been
declared a member function, then it would not compile.
I just noticed that the function I had written was declared inline.
Maybe this is the reason it didn't work?

Can you pass inline functions
to functions which take pointers to functions at all?

As far as inline functions are concerned it is important to have
them defined in every translation unit they are used.
(Section 7.1.2 - Cpp standard)

As long as this rule holds, it is perfectly fine to pass inline
functions to those functions that take a pointer to function.
 

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
473,967
Messages
2,570,148
Members
46,694
Latest member
LetaCadwal

Latest Threads

Top