fork(1) download
  1. #include <algorithm>
  2. #include <iostream>
  3.  
  4. struct student{
  5. char name[20];
  6. int age;
  7. char add[40];
  8. };
  9.  
  10. int main()
  11. {
  12. student stu[5] = {
  13. {"John Doe", 16, "somewhere"},
  14. {"John Smith", 42, "somewhere"},
  15. {"Peter", 18, "somewhere"},
  16. {"Anonymous", 19, "somewhere"},
  17. {"Not me", 21, "somewhere"},
  18. };
  19.  
  20. auto it = std::max_element(std::begin(stu), std::end(stu),
  21. [](const student& lhs, const student& rhs) {
  22. return lhs.age < rhs.age;
  23. });
  24. std::cout << "Oldest is " << it->name << " with " << it->age << std::endl;
  25. return 0;
  26. }
  27.  
  28.  
Success #stdin #stdout 0s 3340KB
stdin
Standard input is empty
stdout
Oldest is John Smith with 42