我想创建一个视图,它包含字符串的两个部分。我添加了一些代码示例来说明我想实现什么。我该怎么做呢?
#include <iostream>
#include <string>
#include <ranges>
#include <vector>
#include <cassert>
int main() {
std::vector<std::string> data{
"abcdef",
"12345678910",
"ab",
"qwerty",
"xyzxyz",
"987654321"
};
// Ok:
auto chunk3 = std::views::chunk(data, 3);
assert(std::ranges::size(chunk3) == 2);
for (auto chunk : chunk3) {
assert(std::ranges::size(chunk) == 3);
}
// Problem:
auto view = /*...*/
assert(std::ranges::size(view) == 6);
for (auto halves : view) {
assert(std::ranges::size(halves) == 2);
}
}
chunk3看起来像什么:
/*
chunk3 {
{"abcdef", "12345678910", "ab"}
{"qwerty", "xyzxyz", "987654321"}
}
*/
视图的外观:
/*
view {
{{"abc"}, {"def"}}
{{"123456"}, {"78910"}}
// ...
}
*/
1条答案
按热度按时间aydmsdu91#
可以使用
views::transform
将原始元素string
转换为包含两个半-string_view
的数组