How to concat two existing ranges::view?

余生颓废 提交于 2019-12-23 02:39:29

问题


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?


回答1:


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.

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());

EDIT

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;


来源:https://stackoverflow.com/questions/43169457/how-to-concat-two-existing-rangesview

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!