| 8 | std::optional<size_t> start_index = std::nullopt); // or: ... start_index = {}); |
| 9 | |
| 10 | int main() |
| 11 | { |
| 12 | const auto string{ "Growing old is mandatory; growing up is optional." }; |
| 13 | |
| 14 | const std::optional<size_t> found_a{ find_last(string, 'a') }; |
| 15 | if (found_a) |
| 16 | std::cout << "Found the last a at index " << *found_a << std::endl; |
| 17 | |
| 18 | const auto found_b{ find_last(string, 'b') }; |
| 19 | if (found_b.has_value()) |
| 20 | std::cout << "Found the last b at index " << found_b.value() << std::endl; |
| 21 | |
| 22 | // following line gives an error (cannot convert std::optional<size_t> to size_t) |
| 23 | // const size_t found_c{ find_last(string, 'c') }; |
| 24 | |
| 25 | const auto found_early_i{ find_last(string, 'i', 10) }; |
| 26 | if (found_early_i != std::nullopt) |
| 27 | std::cout << "Found an early i at index " << *found_early_i << std::endl; |
| 28 | } |
| 29 | |
| 30 | std::optional<size_t> find_last(const std::string& string, char to_find, |
| 31 | std::optional<size_t> start_index) |
nothing calls this directly
no test coverage detected