std::ranges::take_while_view 无法管道
std::ranges::take_while_view cannot pipe
auto int_v10 = std::vector{8,7,3};
//fail auto rng2 = int_v10 | std::ranges::take_while_view([](int x) {return x> 5;} ) | std::ranges::views::common;
auto rng2 = int_v10 | std::ranges::views::take_while([](int x) {return x> 5;} ) | std::ranges::views::common;
auto result4 = std::accumulate(rng2.begin(), rng2.end(),0);
std::cout<<"Result4: "<<result4<<'\n';
https://en.cppreference.com/w/cpp/ranges/take_while_view
为什么 std::ranges::take_while_view
结果无法通过管道传递到下一个代码块。
std::ranges::views::take_while
可以吗?
take_while_view
has only two constructors:
take_while_view() = default;
constexpr take_while_view(V base, Pred pred);
第二个构造函数需要接受两个参数,一个是base,一个是pred,所以需要这样构造:
auto rng2 = std::ranges::take_while_view(int_v10, [](int x) {return x > 5;})
| std::ranges::views::common;
auto int_v10 = std::vector{8,7,3};
//fail auto rng2 = int_v10 | std::ranges::take_while_view([](int x) {return x> 5;} ) | std::ranges::views::common;
auto rng2 = int_v10 | std::ranges::views::take_while([](int x) {return x> 5;} ) | std::ranges::views::common;
auto result4 = std::accumulate(rng2.begin(), rng2.end(),0);
std::cout<<"Result4: "<<result4<<'\n';
https://en.cppreference.com/w/cpp/ranges/take_while_view
为什么 std::ranges::take_while_view
结果无法通过管道传递到下一个代码块。
std::ranges::views::take_while
可以吗?
take_while_view
has only two constructors:
take_while_view() = default;
constexpr take_while_view(V base, Pred pred);
第二个构造函数需要接受两个参数,一个是base,一个是pred,所以需要这样构造:
auto rng2 = std::ranges::take_while_view(int_v10, [](int x) {return x > 5;})
| std::ranges::views::common;