How do I write a range pipeline that uses temporary containers?

前端 未结 6 2423
予麋鹿
予麋鹿 2020-11-30 02:09

I have a third-party function with this signature:

std::vector f(T t);

I also have an existing potentially infinite range (of the

6条回答
  •  Happy的楠姐
    2020-11-30 02:53

    It looks like there are now test cases in the range-v3 library that show how to do this correctly. It is necessary to add the views::cache1 operator into the pipeline:

    auto rng = views::iota(0,4)
            | views::transform([](int i) {return std::string(i, char('a'+i));})
            | views::cache1
            | views::join('-');
    check_equal(rng, {'-','b','-','c','c','-','d','d','d'});
    CPP_assert(input_range);
    CPP_assert(!range);
    CPP_assert(!forward_range);
    CPP_assert(!common_range);
    

    so the solutions for the OP's question would be to write

    auto rng = src | views::transform(f) | views::cache1 | views::join;
    

提交回复
热议问题