Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

std::iota

Use case

Fill range with sequentially increasing values.

Explanation

Fill with value, value+1, … .

Time complexity: O(n). Increments and assigns once per element. See possible implementation.

Code

#include <algorithm>
#include <numeric>
#include <print>
#include <vector>

int main() {
  std::vector<uint64_t> addresses(5);

  std::iota(addresses.begin(), addresses.end(), 0x10000000);

  for (const auto &addr : addresses) {
    std::println("{:#x}", addr);
  }

  return 0;
}

View on GitHub.

Output

$ ./src/algorithms/build/std-iota
0x10000000
0x10000001
0x10000002
0x10000003
0x10000004