| 12 | std::optional<std::string> largest(std::span<const std::string> words); |
| 13 | |
| 14 | int main() |
| 15 | { |
| 16 | const double array[]{ 1.5, 44.6, 13.7, 21.2, 6.7 }; |
| 17 | const std::vector numbers{ 15, 44, 13, 21, 6, 8, 5, 2 }; |
| 18 | const std::vector data{ 3.5, 5.0, 6.0, -1.2, 8.7, 6.4 }; |
| 19 | const std::array array_data{ 3.5, 5.0, 6.0, -1.2, 8.7, 6.4 }; // Throwing in an std::array for good measure |
| 20 | const std::vector<std::string> names{ "Charles Dickens", "Emily Bronte", |
| 21 | "Jane Austen", "Henry James", "Arthur Miller" }; |
| 22 | std::cout << "The largest of array is " << *largest(array) << std::endl; // Crashes if nullopt is returned |
| 23 | std::cout << "The largest of numbers is " << largest(numbers).value() << std::endl; // Throws exception (see Chapter 16) for nullopt |
| 24 | std::cout << "The largest of data is " << largest(data).value() << std::endl; |
| 25 | std::cout << "The largest of array_data is (also) " << *largest(array_data) << std::endl; |
| 26 | std::cout << "The largest of names is " << largest(names).value_or("<null>") << std::endl; |
| 27 | } |
| 28 | |
| 29 | // Finds the largest of a span of values |
| 30 | std::optional<double> largest(std::span<const double> data) |