Is there a compact equivalent to Python range() in C++/STL

后端 未结 10 889
[愿得一人]
[愿得一人] 2020-12-04 21:23

How can I do the equivalent of the following using C++/STL? I want to fill a std::vector with a range of values [min, max).

# Python
>>>         


        
10条回答
  •  半阙折子戏
    2020-12-04 22:10

    If you can't use C++11, you can use std::partial_sum to generate numbers from 1 to 10. And if you need numbers from 0 to 9, you can then subtract 1 using transform:

    std::vector my_data( 10, 1 );
    std::partial_sum( my_data.begin(), my_data.end(), my_data.begin() );
    std::transform(my_data.begin(), my_data.end(), my_data.begin(), bind2nd(std::minus(), 1));
    

提交回复
热议问题