C++ Numeric Library - iota



Description

It is used to store increasing sequence and assigns to every element in the range [first,last) successive values of val, as if incremented with ++val after each element is written.

Declaration

Following is the declaration for std::iota.

C++98

	
template <class ForwardIterator, class T>
  void iota (ForwardIterator first, ForwardIterator last, T val);

C++11

template <class ForwardIterator, class T>
  void iota (ForwardIterator first, ForwardIterator last, T val);
  • first, last − It iterators to the initial and final positions in a sequence.

  • val − It is an initial value for the accumulator.

Return Value

none

Exceptions

It throws if any of the assignments or increments throws.

Data races

The elements in the range [first1,last1) are accessed.

Example

In below example for std::iota.

#include <iostream>
#include <numeric>

int main () {
   int numbers[5];

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

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

   return 0;
}

The output should be like this −

numbers are : 10 11 12 13 14
numeric.htm
Advertisements