std::iota in C++
Last Updated :
02 Feb, 2023
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.
Template :
void iota (ForwardIterator first, ForwardIterator last, T 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 Type :
None
Output
Elements are : 100 101 102 103 104 105 106 107 108 109
Time Complexity: O(n)
Auxiliary Space : O(n)
Application :
It can be used to generate a sequence of consecutive numbers.
C++
#include <iostream> // std::cout
#include <numeric> // std::iota
int main()
{
int numbers[11];
int st = 10;
std::iota(numbers, numbers + 11, st);
std::cout << "Elements are :" ;
for ( auto i : numbers)
std::cout << ' ' << i;
std::cout << '\n' ;
return 0;
}
|
Output
Elements are : 10 11 12 13 14 15 16 17 18 19 20
Time Complexity: O(n)
Auxiliary Space: O(n)
Share your thoughts in the comments
Please Login to comment...