Jump to content
  • Advertisement

Archived

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

kamrann

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

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
std::sort(poly_list.begin(), poly_list.end(), PolygonCompare);?

In addition, why not

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

?

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!