| 5 | #include <algorithm> |
| 6 | |
| 7 | int main() |
| 8 | { |
| 9 | std::vector<std::string> names{"Frodo Baggins", "Gandalf the Gray", |
| 10 | "Aragon", "Samwise Gamgee", "Peregrin Took", "Meriadoc Brandybuck", |
| 11 | "Gimli", "Legolas Greenleaf", "Boromir"}; |
| 12 | |
| 13 | // Sort the names lexicographically |
| 14 | std::sort(begin(names), end(names)); |
| 15 | std::cout << "Names sorted lexicographically:" << std::endl; |
| 16 | for (const auto& name : names) std::cout << name << ", "; |
| 17 | std::cout << std::endl << std::endl; |
| 18 | |
| 19 | // Sort the names by length |
| 20 | std::sort(begin(names), end(names), |
| 21 | [](const auto& left, const auto& right) {return left.length() < right.length(); }); |
| 22 | std::cout << "Names sorted by length:" << std::endl; |
| 23 | for (const auto& name : names) std::cout << name << ", "; |
| 24 | std::cout << std::endl; |
| 25 | } |