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

Function main

Examples/Modules/Chapter 09/Ex9_01.cpp:10–28  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

8 std::optional<size_t> start_index = std::nullopt); // or: ... start_index = {});
9
10int 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
30std::optional<size_t> find_last(const std::string& string, char to_find,
31 std::optional<size_t> start_index)

Callers

nothing calls this directly

Calls 1

find_lastFunction · 0.70

Tested by

no test coverage detected