std::iota
From cppreference.com
C++
Feature test macros (C++20)
Concepts library (C++20)
Metaprogramming library (C++11)
Ranges library (C++20)
Filesystem library (C++17)
Concurrency support library (C++11)
Execution control library (C++26)
Algorithm library
Constrained algorithms, e.g. ranges::copy, ranges::sort, ...
(C++17)
(C++17) (C++17)(C++17)(C++20)
(C++17)
(C++11)
(until C++17)(C++11)
(C++20)(C++20)
(C++17)
(C++17)
(C++17)
(C++17)
Numerics library
Mathematical special functions (C++17)
Mathematical constants (C++20)
Basic linear algebra algorithms (C++26)
Data-parallel types (SIMD) (C++26)
Floating-point environment (C++11)
Bit manipulation (C++20)
Saturation arithmetic (C++26)
(C++17)
(C++17)
(C++17)
(C++17)
(C++17)
(C++17)
Defined in header
<numeric>
template< class ForwardIt, class T >
void iota( ForwardIt first, ForwardIt last, T value );
(since C++11) void iota( ForwardIt first, ForwardIt last, T value );
(constexpr since C++20)
Fills the range [
first,
last)
with sequentially increasing values, starting with value and repetitively evaluating ++value.
Equivalent operation (assuming ++value returns the incremented value):
*first = value; *++first = ++value; *++first = ++value; *++first = ++value; // repeats until "last" is reached
If any of the following conditions is satisfied, the program is ill-formed:
-
T
is not convertible to the value type ofForwardIt
. - The expression ++val is ill-formed, where val is a variable of type
T
.
[edit] Parameters
first, last
-
the pair of iterators defining the range of elements to fill with sequentially increasing values starting with value
value
-
initial value to store
[edit] Complexity
Exactly std::distance (first, last) increments and assignments.
[edit] Possible implementation
template<class ForwardIt, class T> constexpr // since C++20 void iota(ForwardIt first, ForwardIt last, T value) { for (; first != last; ++first, ++value) *first = value; }
[edit] Notes
The function is named after the integer function ⍳ from the programming language APL. It was one of the STL components that were not included in C++98, but made it into the standard library in C++11.
[edit] Example
The following example applies std::shuffle to a vector of std::list s' iterators. std::iota
is used to populate containers.
Run this code
#include <algorithm> #include <iomanip> #include <iostream> #include <list> #include <numeric> #include <random> #include <vector> class BigData // inefficient to copy { int data[1024]; /* some raw data */ public: explicit BigData(int i = 0) { data[0] = i; /* ... */ } operator int() const { return data[0]; } BigData& operator=(int i) { data[0] = i; return *this; } /* ... */ }; int main() { std::list <BigData> l(10); std::iota(l.begin(), l.end(), -4); std::vector <std::list <BigData>::iterator> v(l.size()); std::iota(v.begin(), v.end(), l.begin()); // Vector of iterators (to original data) is used to avoid expensive copying, // and because std::shuffle (below) cannot be applied to a std::list directly. std::shuffle (v.begin(), v.end(), std::mt19937 {std::random_device {}()}); std::cout << "Original contents of the list l:\t"; for (const auto& n : l) std::cout << std::setw (2) << n << ' '; std::cout << '\n'; std::cout << "Contents of l, viewed via shuffled v:\t"; for (const auto i : v) std::cout << std::setw (2) << *i << ' '; std::cout << '\n'; }
Possible output:
Original contents of the list l: -4 -3 -2 -1 0 1 2 3 4 5 Contents of l, viewed via shuffled v: -1 5 -4 0 2 1 4 -2 3 -3
[edit] See also
(C++23)
(algorithm function object)[edit]
assigns the results of successive function calls to every element in a range
(function template) [edit]
(function template) [edit]
(C++20)
view
consisting of a sequence generated by repeatedly incrementing an initial value(class template) (customization point object)[edit]