fork(4) download
  1. #include <string>
  2. #include <iostream>
  3. #include <vector>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7.  
  8. struct Book
  9. {
  10. string Author;
  11. unsigned short PublicationYear;
  12.  
  13. bool operator==(const Book& struc) const
  14. {
  15. return
  16. Author == struc.Author &&
  17. PublicationYear == struc.PublicationYear;
  18. }
  19.  
  20. Book(string author, unsigned short publicationYear) :
  21. Author(author),
  22. PublicationYear(publicationYear)
  23. {
  24. }
  25. };
  26.  
  27. int main()
  28. {
  29. vector<Book> books =
  30. {
  31. Book("Pushkin", 1831),
  32. Book("Blok", 1902),
  33. Book("Yevtushenko", 1962)
  34. };
  35.  
  36. cout << "Sorting by name" << endl;
  37. sort(books.begin(), books.end(), [](const Book& a, const Book& b) {
  38. return a.Author < b.Author;
  39. });
  40.  
  41. for (auto& book : books)
  42. cout << "Author: " << book.Author << ", Year: " << book.PublicationYear << endl;
  43.  
  44. cout << "Sorting by year" << endl;
  45. sort(books.begin(), books.end(), [](const Book& a, const Book& b) {
  46. return a.PublicationYear < b.PublicationYear;
  47. });
  48.  
  49. for (auto& book : books)
  50. cout << "Author: " << book.Author << ", Year: " << book.PublicationYear << endl;
  51.  
  52. return 0;
  53. }
Success #stdin #stdout 0s 2992KB
stdin
Standard input is empty
stdout
Sorting by name
Author: Blok, Year: 1902
Author: Pushkin, Year: 1831
Author: Yevtushenko, Year: 1962
Sorting by year
Author: Pushkin, Year: 1831
Author: Blok, Year: 1902
Author: Yevtushenko, Year: 1962