MCPcopy Create free account
hub / github.com/Apress/beginning-cpp20 / main

Function main

Exercises/NoModules/Chapter 20/Soln20_10/Soln20_10.cpp:7–27  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

5#include <algorithm>
6
7int 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::ranges::sort(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 /* Option 1: projection using member function pointer */
21 std::ranges::sort(names, std::less<>{}, &std::string::length);
22 /* Option 2: projection using lambda expression */
23// std::ranges::sort(names, std::less<>{}, [](const auto& name) { return name.length(); });
24 std::cout << "Names sorted by length:" << std::endl;
25 for (const auto& name : names) std::cout << name << ", ";
26 std::cout << std::endl;
27}

Callers

nothing calls this directly

Calls 1

sortFunction · 0.50

Tested by

no test coverage detected