名前空間
変種
操作

std::iota

提供: cppreference.com
< cpp‎ | algorithm
 
 
アルゴリズムライブラリ
制約付きアルゴリズムと範囲に対するアルゴリズム (C++20)
コンセプトとユーティリティ: std::sortable, std::projected, ...
制約付きアルゴリズム: std::ranges::copy, std::ranges::sort, ...
実行ポリシー (C++17)
非変更シーケンス操作
(C++11)(C++11)(C++11)
(C++17)
変更シーケンス操作
未初期化記憶域の操作
分割操作
ソート操作
(C++11)
二分探索操作
集合操作 (ソート済み範囲用)
ヒープ操作
(C++11)
最小/最大演算
(C++11)
(C++17)

順列
数値演算
C のライブラリ
 
ヘッダ <numeric> で定義
template< class ForwardIt, class T >
void iota( ForwardIt first, ForwardIt last, T value );
(C++11以上)
(C++20未満)
template< class ForwardIt, class T >
constexpr void iota( ForwardIt first, ForwardIt last, T value );
(C++20以上)

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

以下の操作と同等です。

*(d_first)   = value;
*(d_first+1) = ++value;
*(d_first+2) = ++value;
*(d_first+3) = ++value;
...

目次

[編集] 引数

first, last - value から始まり連続的に増加する値で埋める要素の範囲
value - 格納する初期値。 式 ++value が well-formed でなければなりません

[編集] 戻り値

(なし)

[編集] 計算量

ちょうど last - first 回のインクリメントおよび代入。

[編集] 実装例

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

[編集] ノート

この関数はプログラミング言語 APL の整数関数 ⍳ に倣って名付けられました。 これは C++98 に含まれなかった STL のコンポーネントのひとつですが、最終的に C++11 で標準ライブラリに加えられました。

[編集]

以下の例は、 std::list に直接 std::shuffle を適用することができないため、 std::list のイテレータのベクタに std::shuffle を適用します。 std::iota は両方のコンテナに要素を投入するために使用されます。

#include <algorithm>
#include <iostream>
#include <list>
#include <numeric>
#include <random>
#include <vector>
 
int main()
{
    std::list<int> l(10);
    std::iota(l.begin(), l.end(), -4);
 
    std::vector<std::list<int>::iterator> v(l.size());
    std::iota(v.begin(), v.end(), l.begin());
 
    std::shuffle(v.begin(), v.end(), std::mt19937{std::random_device{}()});
 
    std::cout << "Contents of the list: ";
    for(auto n: l) std::cout << n << ' ';
    std::cout << '\n';
 
    std::cout << "Contents of the list, shuffled: ";
    for(auto i: v) std::cout << *i << ' ';
    std::cout << '\n';
}

出力例:

Contents of the list: -4 -3 -2 -1 0 1 2 3 4 5
Contents of the list, shuffled: 0 -1 3 4 -4 1 -2 -3 2 5

[編集] 関連項目

指定された要素を範囲内の全要素にコピー代入します
(関数テンプレート) [edit]
関数を連続的に呼び出した結果を指定範囲の全要素に代入します
(関数テンプレート) [edit]