473,408 Members | 2,832 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,408 software developers and data experts.

sorting std::list with function predicate

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
Jul 22 '05 #1
5 7333
matthias_k wrote:
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
Jul 22 '05 #2
matthias_k wrote:
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.
Jul 22 '05 #3
Karthik Kumar wrote:
matthias_k wrote:
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.


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
Jul 22 '05 #4
Karthik Kumar wrote:
matthias_k wrote:
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.


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?
Jul 22 '05 #5
matthias_k wrote:
Karthik Kumar wrote:
matthias_k wrote:
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.


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.
--
Karthik.
Jul 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: JustSomeGuy | last post by:
I need to write an new class derived from the list class. This class stores data in the list to the disk if an object that is added to the list is over 1K in size. What methods of the std stl...
13
by: Nenad Jalsovec | last post by:
using std::list; struct Something{ Something( val ): value( val ){} bool operator<( Something & s ){ return value < s.value; } int value; }; main(){ list< Something * > things;
11
by: velthuijsen | last post by:
I tried taking a list and pass it through std::sort like the following: sort(Unsorted.begin(), Unsorted.end()); I got an error back stating that the list iterator doesn't have a binary...
5
by: Eric Lilja | last post by:
Hello, consider this complete program (sorry, it's not minimal but I hope it's readable at least): #include <algorithm> #include <iostream> #include <vector> class Row { public:
5
by: Christopher | last post by:
The situation is that a std::list<std::set<std::string is being iterated through. Upon certain criteria some sets become empty. I need to remove the empty sets from the list. Is it safe to...
3
by: jason.cipriani | last post by:
How can I use my own custom comparison function with std::list::sort()? The only call to sort() I see does not take a predicate argument. Specifically, I have: list<pair<double,MyType ...; ...
3
by: Ray D. | last post by:
Hey all, I'm trying to pass a list into a function to edit it but when I compile using g++ I continue to get the following error: maintainNeighbors.cpp:104: error: invalid initialization of...
12
by: isliguezze | last post by:
template <class T> class List { public: List(); List(const List&); List(int, const T&); void push_back(const T &); void push_front(const T &); void pop_back();
17
by: Isliguezze | last post by:
Does anybody know how to make a wrapper for that iterator? Here's my wrapper class for std::list: template <class Tclass List { private: std::list<T*lst; public: List() { lst = new...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.