Reputation: 2793
x
is an unsigned integer. Runtime-wise, what's the fastest and most elegant way to declare a container/initializer list filled with unsigned integers from 0 to x
?
Ideally I'd like the solution to be a one-liner, something along the line of:
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;});
Upvotes: 3
Views: 102
Reputation: 4118
Since C++11, there is a function in the C++ standard library that is made specifically for this: std::iota
Upvotes: 4