fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <algorithm>
  5. using namespace std;
  6.  
  7. class Person
  8. {
  9. string first;
  10. string last;
  11. int age;
  12.  
  13. public:
  14. Person(string first_name, string last_name, int the_age);
  15.  
  16. string getFirstName() const { return first; }
  17. string getLastName() const { return last; }
  18. int getAge() const { return age; }
  19. };
  20.  
  21. Person::Person(string first_name, string last_name, int the_age){
  22. first = first_name;
  23. last = last_name;
  24. age = the_age;
  25. }
  26.  
  27. int main()
  28. {
  29. vector<Person> people;
  30. people.emplace_back("Joe", "Smoe", 25);
  31. people.emplace_back("John", "Cool-Johnson", 15);
  32. people.emplace_back("Paul", "Bob", 1000);
  33.  
  34. vector<string> names;
  35. names.reserve(people.size());
  36.  
  37. for(const Person &p : people) {
  38. names.push_back(p.getFirstName());
  39. }
  40.  
  41. auto max_name = max_element(
  42. names.begin(), names.end(),
  43. [](const string &a, const string &b){
  44. return a.size() < b.size();
  45. }
  46. )->size();
  47.  
  48. cout << max_name;
  49.  
  50. return 0;
  51. }
Success #stdin #stdout 0s 5504KB
stdin
Standard input is empty
stdout
4