Writing Range Pipelines with Temporary Containers
In range-v3, when working with a pipeline that utilizes a third-party function that returns a vector, it is essential to create a pipeline that maps that function to all elements of the range and flattens all the resulting vectors into a single range with all their elements.
Initially, one might attempt to write a pipeline such as:
auto rng = src | view::transform(f) | view::join;
However, this approach was previously not feasible as it is impossible to create views of temporary containers like the ones produced by f.
To address this issue, a patch was introduced that now allows such range pipelines to be written correctly. The key is to add the views::cache1 operator into the pipeline, as seen in the following example:
auto rng = views::iota(0, 4)
| views::transform([](int i) { return std::string(i, char('a' i)); })
| views::cache1
| views::join('-');
This ensures that the pipeline processes the temporary containers correctly, allowing us to write range pipelines that utilize temporary containers effectively.
For the problem described in the question, the solution would be to modify the pipeline as follows:
auto rng = src | views::transform(f) | views::cache1 | views::join;
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3