fork download
  1. #include <iostream>
  2. #include<set>
  3. using namespace std;
  4.  
  5. class Athlete{
  6. public:
  7. string n;
  8. double t;
  9. Athlete(string n, double t) : n(n), t(t) {}
  10. bool operator< (const Athlete a)const {
  11. return t < a.t;
  12. }
  13. };
  14.  
  15. Athlete findFastestRunner(const set<Athlete>& s) {
  16. return *s.begin();
  17. }
  18.  
  19. void printResultsInRange(const set<pair<int, Athlete>>& results, int fromPlace, int toPlace) {
  20. for (set<pair<int, Athlete>>::const_iterator it = results.begin(); it != results.end(); ++it) {
  21. int place = it->first;
  22. if (place >= fromPlace && place <= toPlace) {
  23. cout << place << ". " << it->second.n << " - " << it->second.t << " s" << endl;
  24. }
  25. }
  26. }
  27.  
  28. int main(){
  29. set<Athlete> s;
  30. set<Athlete>::iterator iter;
  31. s.insert(Athlete("James", 45.8)); s.insert(Athlete("Alex", 51.9));
  32. s.insert(Athlete("Nikolas", 47.9)); s.insert(Athlete("Luka", 49.6));
  33. for (const auto& athlete : s) {
  34. cout << athlete.n << " - " << athlete.t << " s" << endl;
  35. }
  36. Athlete f = findFastestRunner(s);
  37. cout << "\Fastest: " << f.n << "; time: " << f.t << " s" << endl;
  38.  
  39. set<pair<int, Athlete>> results;
  40. int place = 1;
  41. for (const auto& athlete : s) {
  42. results.insert({place, athlete});
  43. place++;
  44. }
  45.  
  46. cout << "Result:"<<endl;
  47. for (const auto& res : results) {
  48. cout << res.first << ". " << res.second.n << " - " << res.second.t << " s" << endl;
  49. }
  50.  
  51. cout << "Results from 2nd to 3rd place:" << endl;
  52. printResultsInRange(results, 2, 3);
  53.  
  54.  
  55. return 0;
  56. }
  57.  
  58.  
  59.  
  60.  
  61.  
Success #stdin #stdout 0.01s 5320KB
stdin
Standard input is empty
stdout
James - 45.8 s
Nikolas - 47.9 s
Luka - 49.6 s
Alex - 51.9 s
Fastest: James; time: 45.8 s
Result:
1. James - 45.8 s
2. Nikolas - 47.9 s
3. Luka - 49.6 s
4. Alex - 51.9 s
Results from 2nd to 3rd place:
2. Nikolas - 47.9 s
3. Luka - 49.6 s