iota() in C++ Last Updated : 04 Jun, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report In C++, iota() is a library function used to fill a range of elements with increasing values starting from the given initial value. It assigns the starting value to the first element and then increments it once for the next element and so on.Let's take a look at an example: C++ #include <bits/stdc++.h> using namespace std; int main() { vector<int> v(5); // Assign increasing values starting from 1 iota(v.begin(), v.end(), 1); for (auto i : v) cout << i << " "; return 0; } Output1 2 3 4 5 This article covers the syntax, usage, and common examples of iota() function in C++:Table of ContentSyntax of iota()Examples of iota()Initializing Array Elements using iota()Using iota with Custom Data TypesSyntax of iota()The iota() function is defined inside the <numeric> header file.iota(first, last, val);Parameters:first: Iterator to the first element of the range.last: Iterator to the element just after the last element of the range.val: Starting value.Return Value:This function does not return any value.Note: iota() function only works for those STL containers that support random access using index numbers such as vector, deque, etc.Examples of iota()iota() can be used with any range with data types that have a well-defined increment operation (++). The below examples demonstrate the use of iota() with different containers and its behaviour in different conditions.Initializing Array Elements using iota() C++ #include <bits/stdc++.h> using namespace std; int main() { char arr[5]; int n = sizeof(arr)/sizeof(arr[0]); // Assigning arr elements with sequentially // increasing values starting from 'a' iota(arr, arr + n, 'a'); for (auto i : arr) cout << i << " "; return 0; } Outputa b c d e Using iota with Custom Data Types C++ #include <bits/stdc++.h> using namespace std; struct C { int a; // Defining ++ for struct C C& operator++() { ++a; return *this; } }; int main() { vector<C> v(5); // Fill the vector starting from Counter{1} iota(v.begin(), v.end(), C{1}); for (auto i: v) { cout << i.a << " "; } return 0; } Output1 2 3 4 5 Time Complexity: O(n), where n is the number of elements in the given range.Auxiliary Space: O(1)Explanation: For iota() to work, the ++ operator must be defined for data type used in the range. So, for custom data type, we need to manually implement the ++ operator as done in struct C. Comment More infoAdvertise with us Next Article Operators in C++ S sachin bisht Improve Article Tags : C++ STL cpp-numerics-library Practice Tags : CPPSTL Similar Reads find() in C++ STL C++ find() is a built-in function used to find the first occurrence of an element in the given range. It works with any container that supports iterators, such as arrays, vectors, lists, and more. In this article, we will learn about find() function in C++.C++#include <bits/stdc++.h> using nam 2 min read sort() in C++ STL In C++, sort() is a built-in function used to sort the given range in desired order. It provides a simple and efficient way to sort the data in C++, but it only works on data structures that provide random access to its elements such as vectors and arrays.Let's take a look at an example:C++#include 4 min read Operators in C++ C++ operators are the symbols that operate on values to perform specific mathematical or logical computations on given values. They are the foundation of any programming language.Example:C++#include <iostream> using namespace std; int main() { int a = 10 + 20; cout << a; return 0; }Outpu 9 min read Macros In C++ C++ is a powerful and versatile programming language that offers many features to enhance code flexibility and maintainability. One such feature is macros, which are preprocessor directives used for code generation and substitution. Macros are an essential part of C++ programming and play a crucial 8 min read Unary Operators In C++ In C++, unary operators are the type of operators that work on a single value (operand). They perform operations like changing a value's sign, incrementing or decrementing it by one, or obtaining its address.C++ has a total of 9 unary operators:Table of ContentIncrement Operator (++)Decrement Operat 6 min read std::function in C++ The std::function() in C++ is a function wrapper class which can store and call any function or a callable object. In this article, we will learn about std::function in C++ and how to use it in different cases.Table of ContentWhat is std::function in C++?Example of std::functionMember Functions of s 5 min read Like