Solved: 45 - Span

This commit is contained in:
2026-08-22 11:55:54 +03:00
parent 758b9fdaa8
commit 519e90e66f
+9 -7
View File
@@ -1,21 +1,23 @@
#include "span.hpp" #include "span.hpp"
#include <algorithm>
#include <ranges>
namespace cpp20 { namespace cpp20 {
auto span_sum(std::span<const int> data) -> int { auto span_sum(std::span<const int> data) -> int {
(void)data; return std::ranges::fold_left(data, 0, std::plus<>{});
return 0;
} }
auto first_and_last(std::span<const int> data) -> std::pair<int, int> { auto first_and_last(std::span<const int> data) -> std::pair<int, int> {
(void)data; return {*data.begin(), *(--data.end())};
return {0, 0};
} }
auto make_subspan(std::span<const int> data, std::size_t offset, std::size_t count) auto make_subspan(std::span<const int> data, const std::size_t offset, std::size_t count)
-> std::vector<int> { -> std::vector<int> {
(void)data; (void)offset; (void)count; return std::views::drop(data, offset)
return {}; | std::views::take(count)
| std::ranges::to<std::vector<int>>();
} }
} // namespace cpp20 } // namespace cpp20