From 95afbe5174be402b681cd98ea83cc063cd23f5c0 Mon Sep 17 00:00:00 2001 From: Federico Igne Date: Wed, 20 Dec 2023 00:54:49 +0100 Subject: aoc(2319): Aplenty --- 2023/include/util.h | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) (limited to '2023/include') diff --git a/2023/include/util.h b/2023/include/util.h index 3152719..100c9ed 100644 --- a/2023/include/util.h +++ b/2023/include/util.h @@ -7,6 +7,7 @@ #include #include #include +#include namespace util { @@ -49,7 +50,7 @@ inline void trim(std::string_view& view) * @param the callable object. */ template -void accumulate(std::string_view& view, Accumulate acc) +void accumulate(std::string_view view, Accumulate acc) { size_t from{}, to; std::string elem; @@ -68,6 +69,25 @@ void accumulate(std::string_view& view, Accumulate acc) } } +/** @brief Split a string view according to a delimiter. + * + * Specialized version of `util::accumulate` collecting the split + * `std::string_view` in a `std::vector`. + * + * @tparam delim the delimiter to split the string view. + * @param view the string view. + * + * @see util::accumulate; + */ +template +std::vector split(std::string_view view) +{ + std::vector v; + auto callback = [&v](const std::string_view& s) { v.push_back(s); }; + accumulate(view, callback); + return v; +} + /** @brief Discard element from stream by type. * * @tparam T the type of the element to discard. -- cgit v1.2.3