C++ How to insert a consecutive inter range into std::vector?

You can use std::iota (since C++11).

Fills the range [first, last) with sequentially increasing values, starting with value and repetitively evaluating ++value.

The function is named after the integer function ⍳ from the programming language APL.

e.g.

std::vector<int> result(57 - 23 + 1);
std::iota(result.begin(), result.end(), 23);

With range-v3, it would be:

const std::vector<int> result = ranges::view::ints(23, 58); // upper bound is exclusive

With C++20, std::ranges::iota_view:

const auto result1 = std::ranges::views::iota(23, 58); // upper bound is exclusive
const auto result2 = std::ranges::iota_view(23, 58); // upper bound is exclusive

Tags:

C++

C++17