Codectionary / Developer documentation / C++

C++ std::iota sequences

C++11 iota assigns increasing values into an existing writable range. It is convenient for index lists and consecutive identifiers without a handwritten counter loop.

Syntax

std::iota(values.begin(), values.end(), start);

Examples

C++ std::iota sequences

Save as main.cpp and compile with a C++20 compiler, for example g++ -std=c++20 main.cpp -o example. The article states the minimum language version.

#include <numeric>
#include <vector>
#include <iostream>
int main() {
    std::vector<int> ticketNumbers(4);
    std::iota(ticketNumbers.begin(), ticketNumbers.end(), 101);
    std::cout << ticketNumbers.front() << ' ' << ticketNumbers.back() << '\n';
}
  1. Output: 101 104. Four elements already exist before iota writes them.

Best practices

  • Size the destination first: reserve creates capacity, not writable elements. Ensure repeated increments remain representable.

Runtime & versions

Compiled native applications. Feature availability depends on the language and runtime version used by your project; consult the official reference below.

ISO C++ resources

Specifications & further reading