Resorter Resorter - 4 months ago 27
C++ Question

In C++ how to initialize a vector with values {1,2,...,n} without a loop?

Suppose n is a large integer, how to initialize a vector with {1,2,...,n} without a loop in C++? Thanks.

Answer

As simple as this:

std::vector<int> v( 123 );
std::iota( std::begin( v ), std::end( v ), 1 );