Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

std::views::iota — a lazy sequence of successive values

std::views::iota creates a lazy view of values obtained by incrementing. The two-argument form excludes its end bound, while the one-argument form has no reachable end.

Version: C++23 (repository compiler mode; displayed example verified by execution in an isolated environment)

Syntax

#include <ranges>
std::views::iota(start)
std::views::iota(start, bound)

std::views::iota from <ranges> is a callable object that creates a std::ranges::iota_view. The view generates values during iteration rather than storing the whole sequence in a container. Successive values use ++; there is no step argument.

For integers, std::views::iota(start, bound) represents [start, bound): it includes start but excludes bound. Equal endpoints produce an empty range. The bound must be reachable by incrementing; start > bound does not create a descending sequence and violates a precondition.

std::views::iota(start) has no reachable end. Limit iteration, for example with std::views::take, before exhausting the type's value range. Omitting the bound does not prevent signed integer overflow.

Use matching signedness for integer arguments: std::views::iota(0, 5u) is ill-formed. The example generates 3, 4, 5, 6 and prints them on one line. Use C++23 mode for local verification. The displayed source was run unchanged in an isolated environment, and its standard output matched the stated output exactly.

Examples

#include <iostream>
#include <ranges>

int main() {
    bool first = true;
    for (int value : std::views::iota(3, 7)) {
        if (!first) {
            std::cout << ' ';
        }
        std::cout << value;
        first = false;
    }
    std::cout << '\n';
}

Result

3 4 5 6

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources