Fastest way to assign a range of integers from 0 to x to a container

x

is an unsigned integer. Runtime-how, the fastest and most elegant way to declare a list of containers / initializers filled with integer characters from 0 to x

?

Ideally I would like the solution to be a one-line, something like the line:

std::vector<int> v = {0..x};

      

This is what I have so far, but I'm not sure about the performance:

std::vector<int> v(x);
std::generate_n(v.begin(), x, [] { static int i = -1; ++i; return i;});

      

+3


source to share


1 answer


Since C ++ 11, the C ++ standard library has a function created specifically for this: std::iota



+4


source







All Articles