fork download
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <string>
  4. #include <vector>
  5. #include <algorithm>
  6. using namespace std;
  7.  
  8. class Person
  9. {
  10. string first;
  11. string last;
  12. int age;
  13.  
  14. public:
  15. Person(string first_name, string last_name, int the_age);
  16.  
  17. string getFirstName() const { return first; }
  18. string getLastName() const { return last; }
  19. int getAge() const { return age; }
  20. };
  21.  
  22. Person::Person(string first_name, string last_name, int the_age){
  23. first = first_name;
  24. last = last_name;
  25. age = the_age;
  26. }
  27.  
  28. int main()
  29. {
  30. vector<Person> people;
  31. people.emplace_back("John", "Cool-Johnson", 15);
  32. people.emplace_back("Paul", "Bob", 1000);
  33.  
  34. string::size_type max_fname = 10;
  35. string::size_type max_lname = 9;
  36. string::size_type max_age = 3;
  37.  
  38. for(const Person &p : people)
  39. {
  40. max_fname = max(max_fname, p.getFirstName().size());
  41. max_lname = max(max_lname, p.getLastName().size());
  42. max_age = max(max_age, to_string(p.getAge()).size());
  43. }
  44.  
  45. cout << left << setfill(' ');
  46. cout << setw(max_fname) << "First Name" << " " << setw(max_lname) << "Last Name" << " " << setw(max_age) << "Age" << "\n";
  47. cout << setfill('-');
  48. cout << setw(max_fname) << "" << " " << setw(max_lname) << "" << " " << setw(max_age) << "" << "\n";
  49. cout << setfill(' ');
  50.  
  51. for(const Person &p : people)
  52. {
  53. cout << setw(max_fname) << p.getFirstName() << " " << setw(max_lname) << p.getLastName() << " " << setw(max_age) << p.getAge() << "\n";
  54. }
  55.  
  56. cout << people.size() << " people\n";
  57.  
  58. return 0;
  59. }
Success #stdin #stdout 0s 5524KB
stdin
Standard input is empty
stdout
First Name  Last Name     Age 
----------  ------------  ----
John        Cool-Johnson  15  
Paul        Bob           1000
2 people