C++ std::iota() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
C++11

Definition

In C++, std::iota() fills a range with a growing sequence. You give it a start value, and it writes start, start+1, start+2, and so on, until the range is full.

It is the quickest way to make a vector like 1 2 3 4 5 or 0 1 2 3 without a loop. The name comes from the Greek letter ι in the APL language. It works with numbers, characters, and any type that supports ++. Include <numeric> to use it.

This function is declared in the <numeric> header. It exists since C++11. It is a free function, called as std::iota(...), not with a dot.

Syntax

Give the range to fill and the first value.

C++
std::iota(first, last, value);

std::vector<int> v(5);
std::iota(v.begin(), v.end(), 1);   // 1 2 3 4 5
CallWhat it does
iota(first, last, value)Writes value to the first element, then ++value to the next, and so on, until last.

iota() does not make the range bigger. Create the vector with the size you need first, for example std::vector<int> v(5).

Library declarations
C++
template<class ForwardIt, class T>
void iota(ForwardIt first, ForwardIt last, T value);

// C++20 makes it constexpr.

Parameters

ParameterDescription
first, lastRequired. The range [first, last) to fill. It must already have the size you want.
valueRequired. The first value to write. After each element it is increased with ++value.

Return Value

Return ValueDescription
Nothingiota() returns void. The range is filled in place.

C++ Examples

Example 1 — Fill a Vector with 1 to 5

Make a vector of five slots and let iota() write the numbers.

C++
#include <iostream>
#include <numeric>
#include <vector>

int main() {
    std::vector<int> v(5);

    std::iota(v.begin(), v.end(), 1);

    for (int n : v) {
        std::cout << n << ' ';
    }
    std::cout << '\n';
    return 0;
}

How It Works

  1. std::vector<int> v(5) creates five elements, all 0. iota() needs these slots to exist.
  2. The start value is 1. iota() writes 1 to the first slot, then increases the value by one for each next slot.
  3. When the end is reached the vector holds 1 2 3 4 5. The loop prints it.

Example 2 — Fill a String with Letters

Characters can be counted up too, because ++ works on char.

C++
#include <iostream>
#include <numeric>
#include <string>

int main() {
    std::string letters(5, ' ');

    std::iota(letters.begin(), letters.end(), 'a');

    std::cout << letters << '\n';
    return 0;
}

How It Works

  1. std::string letters(5, ' ') creates a string of five spaces. These are the five slots to be filled.
  2. The start value is the character 'a'. Each ++ moves to the next letter in the alphabet.
  3. The spaces are replaced by a, b, c, d, e, and the program prints abcde.

Example 3 — Make an Index Array and Sort It by Value

A classic trick: fill indexes with iota(), then sort the indexes instead of the data.

C++
#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>

int main() {
    std::vector<int> v = {30, 10, 20};
    std::vector<int> idx(v.size());

    std::iota(idx.begin(), idx.end(), 0);

    std::sort(idx.begin(), idx.end(),
              [&v](int a, int b) { return v[a] < v[b]; });

    std::cout << "sorted order of indexes:";
    for (int i : idx) {
        std::cout << ' ' << i;
    }
    std::cout << '\n';
    return 0;
}

How It Works

  1. iota() fills idx with 0 1 2. These are the positions of the elements in v.
  2. std::sort() reorders idx with a lambda that compares the values v[a] and v[b]. The vector v itself is not moved.
  3. The smallest value 10 sits at index 1, then 20 at index 2, then 30 at index 0. So the indexes end up as 1 2 0.

Frequently Asked Questions

It fills a range with consecutive values. You give a start value, and iota() writes start, start+1, start+2, and so on into the range. It is declared in <numeric>.
Create the vector with size N, then call std::iota(v.begin(), v.end(), 1). For example, std::vector<int> v(10); std::iota(v.begin(), v.end(), 1); gives 1 to 10.
No. It always adds 1 with ++. To get a step of 2, fill with iota() and then change the values, or use std::generate() with your own counter.
iota() only overwrites elements that already exist. An empty vector has none, so nothing happens. Give the vector a size first, for example std::vector<int> v(5).
It comes from the function iota written with the Greek letter ι in the APL language, which makes a list of numbers. The C++ function keeps the name.

Next: lcm()

Return the least common multiple of two integers with <numeric>.

lcm() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

13 people found this page helpful