[ Web Proxy ]
URL:
Viewing: https://en.cppreference.com/cpp/algorithm/iota [Back]  [Original]

std::iota - cppreference.com
cppreference.com
Namespaces
Variants

std::iota

From cppreference.com
This page is about the algorithms for generating increasing integer ranges, known as iota sequences after APL's use of the Greek letter . This is unrelated to the C-style ASCII-to-integer conversion, atoi.
 
 
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Constrained algorithms, e.g. ranges::copy, ranges::sort, ...
Non-modifying sequence operations    
Batch operations
(C++17)
Search operations
Modifying sequence operations
Copy operations
(C++11)
(C++11)
Swap operations
Transformation operations
Generation operations
Removing operations
Order-changing operations
(until C++17)(C++11)
(C++20)(C++20)
Sampling operations
(C++17)

Sorting and related operations
Partitioning operations
(C++11)    

Sorting operations
Binary search operations
(on partitioned ranges)
Set operations (on sorted ranges)
Merge operations (on sorted ranges)
Heap operations
Minimum/maximum operations
(C++11)
(C++17)
Lexicographical comparison operations
Permutation operations


 
 
Defined in header <numeric>
template< class ForwardIt, class T >
void iota( ForwardIt first, ForwardIt last, T value );
(since C++11)
(constexpr since C++20)

Fills the target 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 of ForwardIt.
  • The expression ++val is ill-formed, where val is a variable of type T.

Parameters

first, last - the pair of iterators defining the target range
value - initial value to fill

Complexity

Exactly std::distance(first, last) increments and assignments.

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;
}

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.

Example

The following example applies std::shuffle to a vector of std::lists' iterators. std::iota is used to populate containers.

#include <algorithm>
#include <list>
#include <numeric>
#include <print>
#include <random>
#include <ranges>
#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{}()});

    auto view_l = l | std::views::transform([](const auto& x){ return (int)x; });
    auto view_v = v | std::views::transform([](auto x){ return (int)*x; });

    std::print("Original contents of the list l:\t\t{::2}\n", view_l);
    std::print("Contents of l, viewed via shuffled v:\t{::2}\n", view_v);
}

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: [ 0,  5, -1,  4, -2, -4,  1,  2, -3,  3]

See also

fills a range with successive increments of the starting value
(algorithm function object)[edit]
copy-assigns the given value to every element in a range
(function template & algorithm function object)[edit]
assigns the results of successive function calls to every element in a range
(function template & algorithm function object)[edit]
a view consisting of a sequence generated by repeatedly incrementing an initial value
(class template) (customization point object)[edit]
a view that generates a sequence of increasing values from 0 up to n
(customization point object)[edit]
a view that maps each element of adapted sequence to a tuple of both the element's position and its value
(class template) (range adaptor object)[edit]

Web Proxy Viewer  |  New URL  |  Original Page