fork(10) download
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <tuple>
  4. #include <vector>
  5. using namespace std;
  6.  
  7. void print(vector<tuple<int, string>> const & data) {
  8. for(auto row : data) {
  9. cout << get<string>(row) << " is " << get<int>(row) << " years old.\n";
  10. }
  11. }
  12.  
  13. int main() {
  14. int age;
  15. string name;
  16. vector<tuple<int, string>> data;
  17.  
  18. while(cin >> age >> name) data.emplace_back(age, name);
  19.  
  20. cout << "Unsorted data:\n";
  21. print(data);
  22.  
  23. sort(data.begin(), data.end());
  24. cout << "\nSorted data:\n";
  25. print(data);
  26.  
  27. return 0;
  28. }
Success #stdin #stdout 0s 3468KB
stdin
25 John
12 Sally
15 Robert
23 Betty
29 Don
15 Albert
stdout
Unsorted data:
John is 25 years old.
Sally is 12 years old.
Robert is 15 years old.
Betty is 23 years old.
Don is 29 years old.
Albert is 15 years old.

Sorted data:
Sally is 12 years old.
Albert is 15 years old.
Robert is 15 years old.
Betty is 23 years old.
John is 25 years old.
Don is 29 years old.