Question : I need help with sorting a vector.

This is NOT an academic exercise but an actual problem I'm grappling with.  Hopefully I can get this resolved today.

A bunch of students are participating in a contest where they have to launch an object as far as they can, anywhere from 0 - 100 feet.  Each student gets 4 tries.  The distances are recorded in the struct below along with their Student ID number (which currently ranges from 1-27).   I already have a vector of StudentAttempt structs called vLaunches.

QUESTION: How do I sort the vector such that I find all the TIES between students?  That is, I want to know when 2 or more students have achieved launches of the exact same distance.  I want the output to the screen to look like:

TIES:
44feet: 1, 16, 33     (this indicates that Student ID# 1, 16, 33 all had launches of exactly 44 feet)
32feet: 23, 27
19feet: 2, 3, 9, 14, 17

I am going to need help with understanding the cmpAttempts function which was written by another fellow EE member....
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
struct StudentAttempt
{
    int StudentID
    int distance
}

bool cmpAttempts(StudentAttempt left, StudentAttempt right) {
   if( left.distance < right.distance ) {
      return true;
   }
   else if( left.distance == right.distance ) {
      if( left.StudentID < right.StudentID ) {
         return true;
      }
      else
         return false;
   }
   else
      return false;
}

in the MAIN....
vector <StudentAttempt> vLaunches;

sort( vLaunches.begin(), vLaunches.end(), cmpAttempts);
Related Solutions: How do I sort a vector?

Answer : I need help with sorting a vector.

Using the code in the related post, consider what happens when adjacent_find sends left=(22,A) and right=(22,A) to isDistEqual(). The distances match, but the student ID do not match and so
       left.studentName != right.studentName
is false. So this pair is skipped by adjacent_find.

So, you won't even know from adjacent_find that there even were two entries = (22,A). So, you don't have to worry about getting 22 feet: A, A, C, D since 22,A will be seen only once.
Random Solutions  
 
programming4us programming4us