Set std::vector<int> to a range

49,888

Solution 1

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

std::vector<int> 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<int> v;
boost::push_back(v, boost::irange(3, 17));

Solution 2

std::vector<int> myVec;
for( int i = 3; i <= 16; i++ )
    myVec.push_back( i );

Solution 3

See e.g. this question

#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>

template<class OutputIterator, class Size, class Assignable>
void iota_n(OutputIterator first, Size n, Assignable value)
{
        std::generate_n(first, n, [&value]() {
                return value++;
        });
}

int main()
{
    std::vector<int> v;                   // no default init
    v.reserve(14);                        // allocate 14 ints
    iota_n(std::back_inserter(v), 14, 3); // fill them with 3...16

    std::for_each(v.begin(), v.end(), [](int const& elem) {
        std::cout << elem << "\n";
    });
    return 0;
}

Output on Ideone

Solution 4

std::iota - is useful, but it requires iterator, before creation vector, .... so I take own solution.

#include <iostream>
#include <vector>

template<int ... > struct seq{ typedef seq type;};

template< typename I, typename J> struct add;
template< int...I, int ...J>
struct add< seq<I...>, seq<J...> > : seq<I..., (J+sizeof...(I)) ... >{};


template< int N>
struct make_seq : add< typename make_seq<N/2>::type, 
                       typename make_seq<N-N/2>::type > {};

template<> struct make_seq<0>{ typedef seq<> type; };
template<> struct make_seq<1>{ typedef seq<0> type; };


template<int start, int step , int ... I>
std::initializer_list<int> range_impl(seq<I... > )
{
    return { (start + I*step) ...};
}

template<int start, int finish, int step = 1>
std::initializer_list<int> range()
{ 
    return range_impl<start, step>(typename make_seq< 1+ (finish - start )/step >::type {} ); 
}

int main()
{
    std::vector<int> vrange { range<3, 16>( )} ;

    for(auto x : vrange)std::cout << x << ' ';

}


Output:

  3 4 5 6 7 8 9 10 11 12 13 14 15 16

Solution 5

Try to use std::generate. It can generate values for a container based on a formula

std::vector<int> v(size);
std::generate(v.begin(),v.end(),[n=0]()mutable{return n++;});
Share:
49,888

Related videos on Youtube

Andreas
Author by

Andreas

Updated on February 01, 2020

Comments

  • Andreas
    Andreas about 4 years

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

  • TemplateRex
    TemplateRex over 11 years
    v.reserve(14) would save on the default initialization.
  • juanchopanza
    juanchopanza over 11 years
    @rhalbersma I am not sure that would work. That just changes the internal storage of the vector if necessary, but iota needs a valid iterator range.
  • TemplateRex
    TemplateRex over 11 years
    Not if you use a std::back_inserter
  • jrok
    jrok over 11 years
    With back_inserter_iterator, you don't need to call reserve, don't you?
  • TemplateRex
    TemplateRex over 11 years
    @jrok You don't need to, but it's more efficient. back_inserter will call push_back, but if you insert a large amount of elements, push_back will in turn do a lot of reallocations.
  • juanchopanza
    juanchopanza over 11 years
    @rhalbersma and then when would iota stop? There is no way of telling it "stop after N numbers".
  • JoeG
    JoeG over 11 years
    +1 for KISS. Yours is also the only answer (so far) that you can clearly see the range in the code (loop from 3 to 16 inclusive). The others all use 3 (start of range) and 14 (number of elements).
  • TemplateRex
    TemplateRex over 11 years
    Your code is fine, but I would prefer my own iota_n answer to do the up front memory reservation, rather than default initialization 0...0 and then immediately overwriting with 3...16. What if N = 14 billion in stead of 14?
  • TemplateRex
    TemplateRex over 11 years
    Doing a myVec.reserve(14) upfront would be even better here.
  • juanchopanza
    juanchopanza over 11 years
    @rhalbersma I agree. An option is to use boost::irange.
  • Dirk Holsopple
    Dirk Holsopple over 11 years
    @jrok you never need to call reserve. It's only ever used to avoid reallocations.
  • kuroi neko
    kuroi neko over 8 years
    Ah, but that's a bit too simple for real C++. Your solution is less than 20 lines long, and won't trigger 30 cryptic template errors with a typo :). It will perform unneeded initialization, though, but I suspect the difference will rarely be felt...
  • dgnorton
    dgnorton almost 6 years
    The cryptic part of this example would go in a .h file somewhere that you wouldn't look at often, if ever. I think the use of range<3, 16>() reads nicer than using std::iota.