diff --git a/modules/11_cpp17/src/optional.cpp b/modules/11_cpp17/src/optional.cpp index d52defb..833e58c 100644 --- a/modules/11_cpp17/src/optional.cpp +++ b/modules/11_cpp17/src/optional.cpp @@ -2,19 +2,28 @@ namespace cpp17 { -auto safe_divide(int lhs, int rhs) -> std::optional { - (void)lhs; (void)rhs; - return std::nullopt; +auto safe_divide(const int lhs, const int rhs) -> std::optional { + if (rhs == 0) { + return std::nullopt; + } + return std::optional(lhs / static_cast(rhs)); } auto find_user(const std::vector& users, const std::string& name) -> std::optional { - (void)users; (void)name; - return std::nullopt; + const auto idx = std::ranges::find(users, name); + if (idx == users.end()) { + return std::nullopt; + } + return std::optional(std::distance(users.begin(), idx)); } auto first_positive(const std::vector& data) -> std::optional { - (void)data; + for (const auto& elem : data) { + if (elem > 0) { + return std::optional(elem); + } + } return std::nullopt; }