function template
<numeric>

std::iota

template <class ForwardIterator, class T>
  void iota (ForwardIterator first, ForwardIterator last, T val);
Store increasing sequence
Assigns to every element in the range [first,last) successive values of val, as if incremented with ++val after each element is written.

The behavior of this function template is equivalent to:
1
2
3
4
5
6
7
8
9
template <class ForwardIterator, class T>
  void iota (ForwardIterator first, ForwardIterator last, T val)
{
  while (first!=last) {
    *first = val;
    ++first;
    ++val;
  }
}


Parameters

first, last
Forward iterators to the initial and final positions of the sequence to be written. The range used is [first,last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last.
val
Initial value for the accumulator.

Return value

none

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// iota example
#include <iostream>     // std::cout
#include <numeric>      // std::iota

int main () {
  int init = 100;
  int numbers[10];

  std::iota (numbers,numbers+10,100);

  std::cout << "numbers:";
  for (int& i:numbers) std::cout << ' ' << i;
  std::cout << '\n';

  return 0;
}


Output:

numbers: 100 101 102 103 104 105 106 107 108 109

Complexity

Linear in the distance between first and last (increments and assignments).

Data races

The elements in the range [first,last) are modified (each element is modified exactly once).

Exceptions

Throws if any of the assignments or increments throws.
Note that invalid arguments cause undefined behavior.

See also