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. {"Julie", 8},
  21. {"Douglas", 32},
  22. {"Peter", 16},
  23. {"Mark", 11}
  24. };
  25.  
  26. sort(begin(people), end(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 3464KB
stdin
Standard input is empty
stdout
Julie: 8
Mark: 11
Peter: 16
Douglas: 32