C++20:如何按大小分割范围?
C++20: How to split range by size?
我想将范围 {1, 2, 3, 4, 5}
拆分为 <任意大小> 的子范围范围(例如,大小为 2:{{1, 2}, {3, 4}, {5}}
)。
然而 std::views::split
仅按分隔符拆分。
没有标准的“反向连接”或其他方法吗?
range-v3 调用此算法 chunk
. There is no such range adapter in C++20, but it is part of the set being proposed for C++23 under the same name. For example:
#include <vector>
#include <range/v3/view/chunk.hpp>
#include <fmt/format.h>
#include <fmt/ranges.h>
int main() {
std::vector v = {1, 2, 3, 4, 5};
fmt::print("{}\n", v | ranges::views::chunk(2)); // prints {{1, 2}, {3, 4}, {5}}
}
这似乎是跨语言为该算法选择的非常一致的名称。 Python有chunked, Rust has chunks, Swift has chunks(ofCount: n)
, D has chunks等
我想将范围 {1, 2, 3, 4, 5}
拆分为 <任意大小> 的子范围范围(例如,大小为 2:{{1, 2}, {3, 4}, {5}}
)。
然而 std::views::split
仅按分隔符拆分。
没有标准的“反向连接”或其他方法吗?
range-v3 调用此算法 chunk
. There is no such range adapter in C++20, but it is part of the set being proposed for C++23 under the same name. For example:
#include <vector>
#include <range/v3/view/chunk.hpp>
#include <fmt/format.h>
#include <fmt/ranges.h>
int main() {
std::vector v = {1, 2, 3, 4, 5};
fmt::print("{}\n", v | ranges::views::chunk(2)); // prints {{1, 2}, {3, 4}, {5}}
}
这似乎是跨语言为该算法选择的非常一致的名称。 Python有chunked, Rust has chunks, Swift has chunks(ofCount: n)
, D has chunks等