const int N = 10;
std::vector<int> v;
v.reserve( N );
for ( int i = 1; i <= N; i++ ) v.push_back( i );
Or
const int N = 10;
std::vector<int> v( N );
int i = 1;
for ( int &x : v ) x = i++;
Or
#include <numeric>
//...
const int N = 10;
std::vector<int> v( N );
std::iota( v.begin(), v.end(), 1 );
Or
#include <algorithm>
//...
const int N = 10;
std::vector<int> v( N );
int i = 1;
std::for_each( v.begin(), v.end(), [&i]( int &x ) { x = i++; } );
Or
#include <algorithm>
#include <iterator>
//...
const int N = 10;
std::vector<int> v;
v.reserve( N );
int i = 1;
std::generate_n( std::back_inserter( v ), N, [&i] { return i++; } );
All these methods use for loop
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…