简体   繁体   English

range-v3:使用分度符连接管道范围

[英]range-v3: Joining piped ranges with a delimeter

I'm trying to build a basic demo of the range-v3 library: take some integers, filter out odd values, stringify them, then join those into a comma-separated list. 我正在尝试构建range-v3库的基本演示:取一些整数,过滤掉奇数值,将它们字符串化,然后将它们加入逗号分隔的列表中。 For example, { 8, 6, 7, 5, 3, 0, 9 } becomes "8, 6, 0" . 例如, { 8, 6, 7, 5, 3, 0, 9 }变为"8, 6, 0" From reading the docs and going through examples, it seems like the naïve solution would resemble: 通过阅读文档并通过示例,天真的解决方案看起来像:

string demo(const vector<int>& v)
{
    return v |
        ranges::view::filter([](int i) { return i % 2 == 0; }) |
        ranges::view::transform([](int i) { return to_string(i); }) |
        ranges::view::join(", ");
}

but building on Clang 7 fails with a static assertion that one, "Cannot get a view of a temporary container". 但是在Clang 7上构建时失败,并带有一个静态断言,即“无法获取临时容器的视图”。 Since I'm collecting the result into a string, I can use the eager version - action::join - instead: 由于我将结果收集到字符串中,因此可以使用热切的版本action::join代替:

string demo(const vector<int>& v)
{
    return v |
        ranges::view::filter([](int i) { return i % 2 == 0; }) |
        ranges::view::transform([](int i) { return to_string(i); }) |
        ranges::action::join;
}

but the eager version doesn't seem to have an overload that takes a delimiter. 但热切的版本似乎没有带有分隔符的重载。

Interestingly, the original assertion goes away if you collect join 's inputs into a container first. 有趣的是,如果您首先将join的输入收集到容器中,则原始断言就会消失。 The following compiles and runs fine: 以下代码可以编译并正常运行:

string demo(const vector<int>& v)
{
    vector<string> strings = v |
        ranges::view::filter([](int i) { return i % 2 == 0; }) |
        ranges::view::transform([](int i) { return to_string(i); });
    return strings | ranges::view::join(", ");
}

but this totally defeats the principle of lazy evaluation that drives so much of the library. 但这完全违反了驱动大量库的惰性评估原则。

Why is the first example failing? 为什么第一个示例失败? If it's not feasible, can action::join be given a delimiter? 如果不可行,可以给action::join一个定界符吗?

action::join should accept a delimiter. action::join应该接受一个定界符。 Feel free to file a feature request. 随时提出功能请求。 The actions need a lot of love. 这些动作需要很多爱。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM