Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

std::iota in C++

  • Difficulty Level : Basic
  • Last Updated : 01 Sep, 2021

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 : 

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.
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

C++




// C++ program to illustrate
// std :: iota
#include <iostream> // std::cout
#include <numeric> // std::iota
 
// Driver code
int main()
{
    int numbers[10];
    // Initialising starting value as 100
    int st = 100;
 
    std::iota(numbers, numbers + 10, st);
 
    std::cout << "Elements are :";
    for (auto i : numbers)
        std::cout << ' ' << i;
    std::cout << '\n';
 
    return 0;
}

Output: 

Elements are : 100 101 102 103 104 105 106 107 108 109

Application : 
It can be used to generate a sequence of consecutive numbers. 

C++




// C++ program to generate
// a sequence of numbers using std :: iota
#include <iostream> // std::cout
#include <numeric> // std::iota
 
// Driver code
int main()
{
    int numbers[11];
    // Initialising starting value as 10
    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

This article is contributed by Sachin Bisht. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
 




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!