std::sort

Started by
24 comments, last by daniel_i_l 18 years, 2 months ago
[EDIT] Use source tag and detail explanation. ;)
[EDIT2] Fix the '->' to '.'.

*note*:
1. Your SortDis and SortDisMid are classes.
2. Putting () behind both will create another new instance of the class. That way you are operating on some temporary-created-instance instead of your real object. That's why your stuff never get sorted. Further calling might screw up the stack and causes weird problems. And Your operator() overload hides your mistake.


I suggest you do this way:
class player_group{public:  static bool SortDis( unit const & elem1, unit const & elem2);  static bool SortDisMid( unit const & elem1, unit const & elem2);};bool player_group::SortDis(unit const & elem1, unit const & elem2){  return   abs(elem1.next_step.x-main_path[place_on_path+step].x)+           abs(elem1.next_step.y-main_path[place_on_path+step].y)          < abs(elem2.next_step.x-main_path[place_on_path+step].x)+           abs(elem2.next_step.y-main_path[place_on_path+step].y);	}bool player_group::SortDisMid(unit const & elem1, unit const & elem2){    return   abs(elem1.current_step.x-mid_pos.urrent_step.x)+             abs(elem1.current_step.y-mid_pos.current_step.y)          <  abs(elem2.current_step.x-mid_pos.current_step.x)+             abs(elem2.current_step.y-mid_pos.current_step.y);	}void player_group::SortUnits(int method){  if(method==0)  {     sort(units.begin(), units.end(), SortDis);   }  if(method==1)  {     sort(units.begin(), units.end(), SortDisMid);   }}


[Edited by - DerekSaw on February 10, 2006 2:22:27 AM]
"after many years of singularity, i'm still searching on the event horizon"
Advertisement
Sorry to bother you again...
Now I'm sure that there's a problem with the sorting. I changed the :
vector<unit> units; to:
units[20];

the PGroup.h file to:
extern player_group group //!Changed! :(class player_group{public: //typedef int (*compfn)(const void*, const void*); player_group(); unit units[20];         //!Changed! //vector<unit> units; unit mid_pos; grid main_path[500]; grid src, dest; bool group_regrouped; bool resort; int group_state,number_of_units; int place_on_path,length_of_path; int middle_man; int group_stopped,change_pos_count;  void InitDestination(); void SortUnits(int method); void MoveGroup(); int FindMiddle(); void RegroupUnits(); void FindFormationPositions(); void FindRegroupPath(); void MoveToTargetState(); void RegroupUnitsState(); void RegroupCheck(); };/*  /// !Changed!struct SortDis : public player_group{ bool operator()(const unit & elem1, const unit & elem2)   {    return   abs(elem1.next_step.x-main_path[place_on_path+1].x)+             abs(elem1.next_step.y-main_path[place_on_path+1].y)          <  abs(elem2.next_step.x-main_path[place_on_path+1].x)+             abs(elem2.next_step.y-main_path[place_on_path+1].y) ;	   }};struct SortDisMid : public player_group{ bool operator()(const unit & elem1, const unit & elem2)   {    return   abs(elem1.current_step.x-mid_pos.current_step.x)+             abs(elem1.current_step.y-mid_pos.current_step.y)          <  abs(elem2.current_step.x-mid_pos.current_step.x)+             abs(elem2.current_step.y-mid_pos.current_step.y);	   }};*///!Changed!int CompareDis(struct unit *elem1, struct unit *elem2);int CompareDisMid(struct unit *elem1, struct unit *elem2);//		#endif

And the SortUnits to
void player_group :: SortUnits(int method){  if(method==0)  {   //!Changed!    qsort((void *) &units, number_of_units, sizeof(struct unit), (compfn)CompareDis );   //sort(units.begin(), units.end(), SortDis());  }  if(method==1)  {  //!Changed!   qsort((void *) &units, number_of_units, sizeof(struct unit), (compfn)CompareDisMid );  //sort(units.begin(), units.end(), SortDisMid());  }}


And everything worked perfectly! Why did it work like this and not with the std::sort? I know that I have to change to the std::sort cause qsort cant take members in classes so all the player_groups that I use will have to be defined as externs in the player_group file...total mess:( .
Does anyone know?
Thanks a lot!
"We've all heard that a million monkeys banging on a million typewriters will eventually reproduce the entire works of Shakespeare. Now, thanks to the internet, we know this is not true." -- Professor Robert Silensky
Updated some stuff up there in case you miseed it. Hope it helps you.
"after many years of singularity, i'm still searching on the event horizon"
Thanks, I did what you said (the -> in SortDis should be '.' right?) and I got the following error:

135 C:\Dev-Cpp\Examples\ctf\PGroup.cpp
invalid use of member `player_group::main_path' in static

I think the problem is that I can't use members in static functions - but thats exactly what I want to do (thats why I'm using std and not qsort() )?

Without the static I get pointer to member errors.

Thanks!
"We've all heard that a million monkeys banging on a million typewriters will eventually reproduce the entire works of Shakespeare. Now, thanks to the internet, we know this is not true." -- Professor Robert Silensky
Ooops. You are right. I copy-paste. :P It should be '.'. And I miss that main_path. Sorry.

EDIT: Say, you need lots of data member access. I miss whole lot of that. A better naming next time. ;) (e.g. m_memberdata or _memberdata)

EDIT2: In that case, you do:

class player_group{  void SortUnits(int method)  {     _method = method;     sort(units.begin(), units.end(), *this);  }  bool operator()(const unit & elem1, const unit & elem2)  {    if (_method == 1)    {       return ....;    }    if (_method == 2)    {       return ....;    }  }private:  int _method;};

"after many years of singularity, i'm still searching on the event horizon"
WOW!!
It worked perfectly!
Thanks a lot everyone!!
"We've all heard that a million monkeys banging on a million typewriters will eventually reproduce the entire works of Shakespeare. Now, thanks to the internet, we know this is not true." -- Professor Robert Silensky

This topic is closed to new replies.

Advertisement