#include#include #include using namespace std; int main() { vector numbers = { 3, 1, 4, 1, 5, 9, 2, 6, 5, 3 }; sort(numbers.begin(), numbers.end()); for (int n : numbers) { cout << n << " "; } return 0; }
#includeThis code sorts a vector of Person objects by age in ascending order using std::sort. It uses a comparison function to compare Person objects by age. The std::sort function is provided by the C++ Standard Library.#include #include using namespace std; struct Person { string name; int age; }; bool compareByAge(Person p1, Person p2) { return p1.age < p2.age; } int main() { vector people = { { "Alice", 23 }, { "Bob", 34 }, { "Charlie", 26 }, { "David", 18 } }; sort(people.begin(), people.end(), compareByAge); for (Person p : people) { cout << p.name << " (" << p.age << ")" << endl; } return 0; }