Answered step by step
Verified Expert Solution
Question
1 Approved Answer
How would I add a line to the main function to sort the people vector by Age using the STL sort function? #include #include #include
How would I add a line to the main function to sort the people vector by Age using the STL sort function?
#include#include #include #include using namespace std; struct Person { string name; int age; string favoriteColor; }; bool sortByName(Person &lhs, Person &rhs) { return lhs.name < rhs.name; } bool sortByAge(Person &lhs, Person &rhs) { return lhs.age < rhs.age; } bool sortByColor(Person &lhs, Person &rhs) { return lhs.favoriteColor < rhs.favoriteColor; } int main() { vector people(5); for (int i = 0; i< 5; i++) { cout << "Person #" << i + 1 << " name: "; cin >> people[i].name; cout << "Person #" << i + 1 << " age: "; cin >> people[i].age; cout << "Person #" << i + 1 << " favorite color: "; cin >> people[i].favoriteColor; } //call STL sort function here! }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started