C++ STL
iota_n algorithm
programming
software development
algorithms

What would be a good implementation of iota_n missing algorithm from the STL

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

std::iota fills a range with increasing values, but it assumes you already have a begin and end iterator. A useful iota_n would be the counted counterpart: write n sequential values starting from some initial value, which makes it feel like the std::generate_n version of std::iota.

What iota_n Should Probably Do

The most natural behavior is:

  • accept an output iterator,
  • accept a count,
  • accept a starting value,
  • write exactly that many incrementing values,
  • return the advanced iterator.

That gives you a clean count-based algorithm without needing a pre-existing bounded range.

A direct implementation is simple:

cpp
1#include <cstddef>
2#include <iostream>
3#include <vector>
4
5template <class OutputIt, class Size, class T>
6OutputIt iota_n(OutputIt first, Size count, T value) {
7    for (Size i = 0; i < count; ++i, ++first, ++value) {
8        *first = value;
9    }
10    return first;
11}
12
13int main() {
14    std::vector<int> values(5);
15    iota_n(values.begin(), values.size(), 10);
16
17    for (int value : values) {
18        std::cout << value << ' ';
19    }
20}

This is probably the best starting point because it is explicit, efficient, and easy to read.

Why This Is Different from std::iota

If you already have a normal range, then plain std::iota is enough:

cpp
1#include <iostream>
2#include <numeric>
3#include <vector>
4
5int main() {
6    std::vector<int> values(5);
7    std::iota(values.begin(), values.end(), 3);
8
9    for (int value : values) {
10        std::cout << value << ' ';
11    }
12}

The count-based form becomes useful when your output is driven by "write n values" rather than "fill until this iterator." That matters for output iterators, generated destinations, and APIs that naturally provide a length instead of an end iterator.

Building It from std::generate_n

Another reasonable implementation uses std::generate_n, because the control structure is already exactly what iota_n needs.

cpp
1#include <algorithm>
2#include <iostream>
3#include <vector>
4
5template <class OutputIt, class Size, class T>
6OutputIt iota_n(OutputIt first, Size count, T value) {
7    return std::generate_n(first, count, [value]() mutable {
8        return value++;
9    });
10}
11
12int main() {
13    std::vector<int> values(4);
14    iota_n(values.begin(), values.size(), 1);
15
16    for (int value : values) {
17        std::cout << value << ' ';
18    }
19}

This is concise and idiomatic. It also makes the relationship to the rest of the STL very obvious: iota_n is really just sequential generation with remembered state.

Should It Support a Step Argument?

Some people asking for iota_n really want an arithmetic progression algorithm, not only a counted copy of std::iota. In that case, adding a step value is useful.

cpp
1#include <iostream>
2#include <vector>
3
4template <class OutputIt, class Size, class T>
5OutputIt iota_n(OutputIt first, Size count, T value, T step) {
6    for (Size i = 0; i < count; ++i, ++first) {
7        *first = value;
8        value += step;
9    }
10    return first;
11}
12
13int main() {
14    std::vector<int> values(5);
15    iota_n(values.begin(), values.size(), 0, 2);
16
17    for (int value : values) {
18        std::cout << value << ' ';
19    }
20}

That is a useful extension, but it is no longer just the missing counted form of std::iota. It is a more general progression algorithm.

Common Pitfalls

  • Reimplementing iota_n when ordinary std::iota already solves the problem for a normal range.
  • Returning void instead of the advanced iterator.
  • Overcomplicating the design when a simple loop is already correct and efficient.
  • Confusing a counted iota with a more general stepped sequence generator.
  • Forgetting that the counted form is most useful with output iterators and APIs that naturally work with lengths.

Summary

  • A good iota_n is the count-based counterpart to std::iota.
  • It should write n sequential values beginning with a supplied starting value.
  • A simple loop implementation is perfectly good STL-style code.
  • An implementation based on std::generate_n is also elegant and idiomatic.
  • If you add a step parameter, the algorithm becomes more general than a strict missing std::iota companion.

Course illustration
Course illustration

All Rights Reserved.