Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags


How to use the iota() function in C++

Sumit Sharma

The iota() function is available in the numeric header file in C++. iota() is used to assign successive values of value to every element in a specified range. The value gets incremented by 1 after assignment to an element.


The iota() method takes the following parameters:

  • First: Forward iterator for the initial position of the sequence to be written.
  • Last: Forward iterator for the final position of the sequence to be written.
  • Value: Initial value for the accumulator.

Forward iterators are iterators that can be used to access the sequence of elements in a range in the direction that goes from its beginning towards its end.

Return value

There is no return value.


Let’s understand with the help of an example.

  • Array = num[16]

  • Range = [num,num+16) i.e from num[0] to num[15]

  • Value = 19

We have an array of size = 16, i.e., we can place the elements from index 0 to index 15. For the range, the iota() function includes the element pointed by first but excludes the element pointed by last.

The value in the accumulator is 19, which will get incremented by 1 after every assignment of value to the element until its range is met.

The result of the iota() function is:

  • 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34


Let’s look at the code snippet.

#include <iostream>
#include <numeric>
using namespace std;
int main()
    int num[16];
    int val;
    cin >> val;
    cout<<"Elements are: ";
    for(auto i:num)
        cout<<' '<<i;
    return 0;

Enter the input below

Using iota() function in C++


  • In lines 1 and 2, we import the required header files.
  • In line 4, we make a main function.
  • In line 6, we declare an array of int type.
  • In line 7, we declare a variable of int type.
  • In line 8, we take the input of int type.
  • In line 9, we use the iota() function to assign the sequence of elements in the array.
  • In line 10, we display a message about the upcoming result.
  • In lines 11 to 14, we use the for loop to access the elements of the array and display them as a result.

In this way, we use the iota() function to generate the sequence of numbers in a specified range.



View all Courses

Keep Exploring