Namespaces
Variants

std:: iota

From cppreference.net
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Constrained algorithms, e.g. ranges::copy , ranges::sort , ...
Execution policies (C++17)
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
Sorting operations
Binary search operations
(on partitioned ranges)
Set operations (on sorted ranges)
Merge operations (on sorted ranges)
Heap operations
Minimum/maximum operations
Lexicographical comparison operations
Permutation operations
C library
Numeric operations
Operations on uninitialized memory
ヘッダーで定義 <numeric>
template < class ForwardIt, class T >
void iota ( ForwardIt first, ForwardIt last, T value ) ;
(C++11以降)
(C++20以降 constexpr)

範囲 [ first , last ) を、 value から始まり、 ++ value を繰り返し評価することで連続的に増加する値で埋める。

同等の操作( ++ value がインクリメント後の値を返すと仮定した場合):

*first   = value;
*++first = ++value;
*++first = ++value;
*++first = ++value;
// 「last」に達するまで繰り返す

以下のいずれかの条件が満たされる場合、プログラムは不適格となります:

  • T ForwardIt value type に変換可能でない。
  • ++ val は不適格(ill-formed)である。ここで val は型 T の変数である。

目次

パラメータ

first, last - 要素を順次増加する値で埋める範囲を定義するイテレータのペア。値は value から開始
value - 格納する初期値

計算量

正確に std:: distance ( first, last ) 回のインクリメントと代入が行われます。

実装例

template<class ForwardIt, class T>
constexpr // C++20以降
void iota(ForwardIt first, ForwardIt last, T value)
{
    for (; first != last; ++first, ++value)
        *first = value;
}

注記

この関数はプログラミング言語 APL の整数関数 に因んで命名されました。これはC++98には含まれていなかった STLコンポーネント の一つですが、C++11で標準ライブラリに組み込まれました。

以下の例は、 std::shuffle vector std::list イテレータに適用します。 std::iota はコンテナの初期化に使用されます。

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

出力例:

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

関連項目

範囲を開始値からの連続的な増分値で埋める
(アルゴリズム関数オブジェクト)
指定された値を範囲内の全要素にコピー代入する
(関数テンプレート)
範囲の要素に特定の値を代入する
(アルゴリズム関数オブジェクト)
連続する関数呼び出しの結果を範囲内の全要素に代入する
(関数テンプレート)
関数の結果を範囲に保存する
(アルゴリズム関数オブジェクト)
初期値を繰り返しインクリメントして生成されるシーケンスからなる view
(クラステンプレート) (カスタマイゼーションポイントオブジェクト)
適合されたシーケンスの各要素を、要素の位置と値の両方を含むタプルにマッピングする view
(クラステンプレート) (レンジアダプタオブジェクト)