Set std::vector to a range

后端 未结 5 924
天涯浪人
天涯浪人 2020-12-29 03:37

What\'s the best way for setting an std::vector to a range, e.g. all numbers between 3 and 16?

5条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-29 03:39

    You could use std::iota if you have C++11 support or are using the STL:

    std::vector v(14);
    std::iota(v.begin(), v.end(), 3);
    

    or implement your own if not.

    If you can use boost, then a nice option is boost::irange:

    std::vector v;
    boost::push_back(v, boost::irange(3, 17));
    

提交回复
热议问题