4

考虑以下情况:

std::vector<int> v{0, 1, 2, 3, 4, 5};
// 0 1 2 3 4 5
auto rng1 = std::views::all(v);
// 5 4 3 2 1 0
auto rng2 = std::views::reverse(v);
// 4 2 0
auto rng3 = std::views::filter(rng2, [](int x){return x % 2 == 0;});

有没有一种优雅的方法可以将这三个适配器连接到一个视图中,如下所示:

// 0 1 2 3 4 5 5 4 3 2 1 0 4 2 0
auto final_rng = std::views::concat(rng1, rng2, rng3);

这似乎是不可能的,因为rng1,rng2rng3's 是非常不同的类型。

有人可以提供替代解决方案吗?谢谢。

4

1 回答 1

4

Yes, what you wrote just in a different namespace - there's no concat in the Standard Library but there is one in range-v3:

auto final_rng = ranges::views::concat(rng1, rng2, rng3);

The fact that the ranges are different types doesn't pose a problem. You just have an iterator that's built up of a variant of the underlying iterators of the ranges. The important part is that the value type of the ranges is the same - and here it is, so that's totally fine.

于 2020-06-18T18:05:17.680 回答