Generating functors with iterator behavior

前端 未结 4 1729
温柔的废话
温柔的废话 2021-02-04 13:53

I have a question, which very likely has been asked like this before, because I think what I want is something that a considerable amount of people would want. However I could n

4条回答
  •  南方客
    南方客 (楼主)
    2021-02-04 14:21

    The Boost.Range way to solve this problem is to use the transform iterator adaptor:

    auto rng = boost::irange(1, 10)
        | boost::adaptors::transformed([](int i) { return i * i; });
    std::vector v{rng.begin(), rng.end()};
    

    Note how this separates the concerns of the transformation from the start/stop/step (optional) parameters of the input range.

提交回复
热议问题