fork(1) download
  1. #include <iostream>
  2. #include <string>
  3. #include <algorithm>
  4. using namespace std;
  5.  
  6. struct person {
  7. string name;
  8. int age;
  9. };
  10. typedef person const &person_view;
  11.  
  12. namespace compare_person {
  13. bool by_age(person_view lhs, person_view rhs) {
  14. return lhs.age < rhs.age;
  15. }
  16. }
  17.  
  18. int main() {
  19. person people[] = {
  20. {"Jasiek ", 26},
  21. {"Stasiek", 53},
  22. {"Piotrek", 16},
  23. {"Pawel", 19}
  24. };
  25.  
  26. sort(people, people + sizeof people / sizeof *people,compare_person::by_age);
  27.  
  28. for(person_view person: people) {
  29. cout << person.name << ": " << person.age << "\n";
  30. }
  31.  
  32. return 0;
  33. }
Success #stdin #stdout 0s 3416KB
stdin
Standard input is empty
stdout
Piotrek: 16
Pawel: 19
Jasiek : 26
Stasiek: 53