mardi 28 juillet 2015

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

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;});

Aucun commentaire:

Enregistrer un commentaire