stable_sort with arrays?

  • Thread starter Andreas Schmitt
  • Start date
A

Andreas Schmitt

Is it possible to do something like this:

Team* League[4];

for (int team=0; team<4; team++)

{

League[team] = new Team;

}

std::stable_sort(League[0], League[3], TeamIsBetter );



***********************************

bool TeamIsBetter( Team* Team1, Team* Team2 )

{

int PointsTeam1 = Team1->GetPoints();

int PointsTeam2 = Team2->GetPoints();


return PointsTeam2 > PointsTeam1;

}
 
K

Kai-Uwe Bux

Andreas said:
Is it possible to do something like this:

Team* League[4];

for (int team=0; team<4; team++)

{

League[team] = new Team;

}

std::stable_sort(League[0], League[3], TeamIsBetter );

Use:

std::stable_sort(League+0, League+4, TeamIsBetter );

a) Pointers into an array can be used as iterators in generic algorithms
from the standard library.

b) std::stable_sort wants the second parameter to point *past* the end.

***********************************

bool TeamIsBetter( Team* Team1, Team* Team2 )

{

int PointsTeam1 = Team1->GetPoints();

int PointsTeam2 = Team2->GetPoints();


return PointsTeam2 > PointsTeam1;

}


Best

Kai-Uwe Bux
 

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,766
Messages
2,569,569
Members
45,045
Latest member
DRCM

Latest Threads

Top