Question : How do I sort a vector?

This is NOT an academic exercises but a real-world problem I'm trying to solve.  

I have a bunch of students in a contest to see who can launch an object the farthest.  The distance may vary anywhere from 0 to 50 ft.  Each student gets 4 tries.  The data is stored in a vector of  structs (see code).

Let's pretend there are 5 students for this example, but the number of students may actually vary in the future.  So if there are 5 students and each get 4 tries...how do I sort all 20 tries by distance?

Thanks!
1:
2:
3:
4:
5:
6:
7:
8:
9:
struct StudentAttempts
{
     int ThrowOne;
     int ThrowTwo;
     int ThrowThree;
     int ThrowFour;
}

vector <StudentAttempts> vThrows;

Answer : How do I sort a vector?

>> If I want to continue down the path of using my using my vThrows vector which contains structs with 4 throws and the students name

Outside of the new function, you can do this. Inside this function, you take one of your vector elements having 4 throws, and copy them into 4 vector elements each having one throw. The benefit is that you save a lot of effort in the sorting and matching using the two algorithm STL functions that I used.

So, I think you should accept these lines:
vector<StudentAttempt> dist(...);
sort( dist.begin(), dist.end(), cmpAttempts);

Just loop over your input vector and for each of your input elements, you have four smaller records which you push into the dist vector. One loop transfers the data. Then sort, and you are up-to-speed.
Random Solutions  
 
programming4us programming4us