#include#include using namespace std; int main() { list
mylist = {3, 7, 1, 9, 4, 8, 2, 6, 5}; mylist.sort(); // sort the list for (auto& x : mylist) { cout << x << " "; } cout << endl; return 0; }
#includeThe output of this program will be: "pear orange kiwi banana apple". In both examples, the "list" and "algorithm" packages (or headers) are used in addition to the "iostream" package for input/output.#include #include
using namespace std; bool compare(const string& a, const string& b) { return a > b; // sort in descending order } int main() { list mylist = {"apple", "banana", "pear", "orange", "kiwi"}; mylist.sort(compare); // sort the list using the custom comparison function for (auto& x : mylist) { cout << x << " "; } cout << endl; return 0; }