简体   繁体   English

Range-v3:为什么这里需要 range::to_vector?

[英]Range-v3: Why is ranges::to_vector needed here?

I'm trying to compute a reversed views::partial_sum .我正在尝试计算一个反向的views::partial_sum The code below gives the expected result of a non-reversed partial_'min', but I need to use ranges::to_vector in order to un- views::reverse the final result (since you can't views::reverse a views::partial_sum ).下面的代码给出了非反转partial_'min'的预期结果,但我需要使用ranges::to_vectorranges::to_vector views::reverse最终结果(因为你不能views::reverse a views::partial_sum )。 However, when the second to_vector is uncommented, the views::values of intermediate2 are all zeros (although the keys are computed correctly).然而,当第二个to_vector被取消注释时, intermediate2views::values全为零(尽管键被正确计算)。 Uncommenting the first to_vector will fix this, but I'd like to know why?取消注释第一个to_vector将解决此问题,但我想知道为什么? And whether I it is possible to avoid the first to_vector ?我是否有可能避免第一个to_vector Or whether should I just not bother with understanding and just chuck in to_vector s until the code works.或者我是否应该to_vector理解而只是在代码工作之前加入to_vector s。

    auto input = std::vector<float>{} | actions::push_back(views::iota(0u, COUNT)) | actions::shuffle(std::default_random_engine{});;

    auto intermediate1 = views::zip(views::iota(0u, COUNT), input)
        //| to_vector
        ;
    auto intermediate2 = intermediate1
        | views::reverse
        | views::partial_sum(
            [](std::pair<unsigned, float> a, std::pair<unsigned, float> b)
            {
                if (a.second > b.second)
                    return b;
                else
                    return a;
            })
        //| to_vector
        ;
    auto ans = intermediate2
        //| views::reverse
        ;

    std::cout << "values  = " << (ans | ranges::views::values | views::take(23)) << std::endl;
    std::cout << "indices = " << (ans | ranges::views::keys | views::take(23)) << std::endl;

This is specifically a problem with views::parital_sum as modifying the comments as follows这特别是views::parital_sum一个问题,因为修改注释如下

        //| views::partial_sum(
        //    [](std::pair<unsigned, float> a, std::pair<unsigned, float> b)
        //    {
        //        if (a.second > b.second)
        //            return b;
        //        else
        //            return a;
        //    })
        | to_vector

will give me the expected result.会给我预期的结果。

This is a range-v3 bug that is a result of views::reverse not properly propagating the value_type , and so you end up with a vector<common_pair<unsigned int, float&>> (note the reference) where you should really be ending up with a vector<pair<unsigned int, float>> .这是一个 range-v3 错误,是由于views::reverse没有正确传播value_type ,所以你最终得到了一个vector<common_pair<unsigned int, float&>> (注意参考),你应该真正结束加上vector<pair<unsigned int, float>> This will be fixed by this PR .这将由这个 PR修复。

In the meantime, you can fix this yourself by providing an explicit type for the vector that you're converting into, via ranges::to<std::vector<std::pair<unsigned int, float>>>() .同时,您可以通过为要转换成的vector提供显式类型来解决这个问题,方法是通过ranges::to<std::vector<std::pair<unsigned int, float>>>()

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

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