Is there a compact equivalent to Python range() in C++/STL

In C++11, there’s std::iota:

#include <vector>
#include <numeric> //std::iota

int main() {
    std::vector<int> x(10);
    std::iota(std::begin(x), std::end(x), 0); //0 is the starting number
}

C++20 introduced a lazy version (just like Python) as part of the ranges library:

#include <iostream>
#include <ranges>

namespace views = std::views;

int main() {
    for (int x : views::iota(0, 10)) {
        std::cout << x << ' '; // 0 1 2 3 4 5 6 7 8 9
    }
}

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)