Solved: 40 - Optional
This commit is contained in:
@@ -2,19 +2,28 @@
|
|||||||
|
|
||||||
namespace cpp17 {
|
namespace cpp17 {
|
||||||
|
|
||||||
auto safe_divide(int lhs, int rhs) -> std::optional<double> {
|
auto safe_divide(const int lhs, const int rhs) -> std::optional<double> {
|
||||||
(void)lhs; (void)rhs;
|
if (rhs == 0) {
|
||||||
return std::nullopt;
|
return std::nullopt;
|
||||||
}
|
}
|
||||||
|
return std::optional(lhs / static_cast<double>(rhs));
|
||||||
|
}
|
||||||
|
|
||||||
auto find_user(const std::vector<std::string>& users, const std::string& name)
|
auto find_user(const std::vector<std::string>& users, const std::string& name)
|
||||||
-> std::optional<std::size_t> {
|
-> std::optional<std::size_t> {
|
||||||
(void)users; (void)name;
|
const auto idx = std::ranges::find(users, name);
|
||||||
|
if (idx == users.end()) {
|
||||||
return std::nullopt;
|
return std::nullopt;
|
||||||
}
|
}
|
||||||
|
return std::optional<std::size_t>(std::distance(users.begin(), idx));
|
||||||
|
}
|
||||||
|
|
||||||
auto first_positive(const std::vector<int>& data) -> std::optional<int> {
|
auto first_positive(const std::vector<int>& data) -> std::optional<int> {
|
||||||
(void)data;
|
for (const auto& elem : data) {
|
||||||
|
if (elem > 0) {
|
||||||
|
return std::optional(elem);
|
||||||
|
}
|
||||||
|
}
|
||||||
return std::nullopt;
|
return std::nullopt;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user