#### Archived

This topic is now archived and is closed to further replies.

# How to use std::sort and std::list::sort?

This topic is 5650 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Can someone give me an example of how to use the predicate version of either of these. I''ve tried all sorts of things and VC++ keeps throwing hundreds of errors at me. I have a class CEditorPoly, and a list of pointers std::list, which I want to sort based on the fields of CEditorPoly. One way I tried was to define a function bool PolygonCompare(const CEditorPoly*& p1, const CEditorPoly*& p2) { ... } and call std::sort(begin, end, PolygonCompare), but this gave me loads of "could not deduce template argument" errors. Cheers Cameron

##### Share on other sites
std::sort(poly_list.begin(), poly_list.end(), PolygonCompare);?

bool PolygonCompare(const CEditorPoly* p1,
const CEditorPoly* p2)
{
...
}

?

##### Share on other sites
In addition to do sorts without having to supply a function to do the sorting just add a operator like this:

  inline bool CThread::operator < (const CThread &a_Thread){	return m_Schedule < a_Thread.m_Schedule;}

Taken directly from my virtual threading code. This sorts the threads based on who needs to run next. You can do this with your ~polylist class.

HTH

Chris Brodie
http:\\fourth.flipcode.com

##### Share on other sites
Your arguments are pointers to references. No-no. Furthermore, std::list has its own sort member function, and it''s advisable to use that instead (You''ll simply need to provide operator <, as gimp mentioned).

##### Share on other sites
No, they''re references to pointers. I did this since somewhere in the stl docs to do with sorting I saw "const T&" was used as the argument type, and here T is "CEditorPoly*". Anyway, I''ve also already tried without the references, and got the errors I gave in my first post. As for operator <, I can''t seem to use it since my elements are pointers. Are you guys using VC++6 or not? Maybe thats the cause.
Basically I did exactly as IndirectX wrote in his post (with the Compare function above the sort call though) and it didn''t compile.
Any ideas why not?

Cameron

• ### Forum Statistics

• Total Topics
628645
• Total Posts
2984028

• 9
• 9
• 9
• 10
• 21