fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <algorithm>
  5. using namespace std;
  6.  
  7. class Movie {
  8. public:
  9. Movie(string movieTitle);
  10. void SetVotes(int numUpVotes, int numDownVotes) {
  11. upVotes = numUpVotes;
  12. downVotes = numDownVotes;
  13. }
  14. string GetTitle() const { return title; }
  15. int GetVoteDifference() const { return upVotes - downVotes; }
  16.  
  17. private:
  18. string title;
  19. int upVotes;
  20. int downVotes;
  21. };
  22.  
  23. Movie::Movie(string movieTitle) {
  24. title = movieTitle;
  25. upVotes = 0;
  26. downVotes = 0;
  27. }
  28.  
  29. bool operator<(const Movie& movie1, const Movie& movie2) {
  30. return movie1.GetVoteDifference() < movie2.GetVoteDifference();
  31. }
  32.  
  33. int main() {
  34. vector<Movie> movieList;
  35. Movie movie1("Batman");
  36. Movie movie2("Up");
  37. Movie movie3("It");
  38. Movie movie4("Frozen");
  39.  
  40. movie1.SetVotes(9, 4);
  41. movie2.SetVotes(15, 2);
  42. movie3.SetVotes(14, 1);
  43. movie4.SetVotes(13, 2);
  44.  
  45. movieList.push_back(movie1);
  46. movieList.push_back(movie2);
  47. movieList.push_back(movie3);
  48. movieList.push_back(movie4);
  49.  
  50. sort(movieList.begin(), movieList.end());
  51.  
  52. cout << movieList.back().GetTitle() << " is the best." << endl;
  53.  
  54. return 0;
  55. }
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
It is the best.