Say I want all numbers from 23 to 57 be in a vector. I can do this:
vector<int> result;
for (int i = 23; i <= 57; ++i)
{
    result.push_back(i);
}
But this is a 5 line solution for a simple job. Can't I do that more elegantly? The best syntax would be vector<int> result{23 .. 57}; for example or such a trivial one line code. Any options with C++17? 
You can use std::iota (since C++11).
Fills the range [first, last) with sequentially increasing values, starting with
valueand 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
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With