如何连接两个现有范围::视图? [英] How to concat two existing ranges::view?
问题描述
我想使用现有视图进行串联.在代码中:
I want to use an existing view for concatenation. In code:
auto rng = view::empty<vector<int>>();
for(int i{0}; i < 5; ++i)
{
vector<int> const & v{foo()}; // returns a reference
rng |= view::concat(v); // doesn't compile - error: no viable overloaded '|='
};
换句话说-我如何创建多个矢量的视图,这些矢量的编号直到运行时才知道?
In other words - how can I create a view to multiple vectors whose number is not known until runtime?
推荐答案
您不能以这种方式编写视图.串联视图将产生具有不同类型的对象.您无法将其分配回原始视图,因为其类型不同.
You can't compose views this way. Concatenating a view yields an object with a different type. You can't assign it back to the original view because its type is different.
您可以结合使用view::cycle
(采用一个范围并无限重复)和view::take
(采用范围的第一个 N 元素)来获得想要的效果.
You can get the effect you're after with a combination of view::cycle
(takes one range and repeats it infinitely), and view::take
(takes the first N elements of a range).
vector<int> const & v{foo()}; // returns a reference
auto rng = v | view::cycle | view::take(5 * v.size());
编辑
如果foo()
每次都可以返回对不同向量的引用,那么除了view::take
之外,您还可以使用view::generate
和view::join
:
If foo()
can return a reference to a different vector each time, then you can use view::generate
and view::join
, in addition to view::take
:
auto rng = view::generate(foo) | view::take(5) | view::join;
这篇关于如何连接两个现有范围::视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!