std:: iota
| Common mathematical functions | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Complex numbers | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Numeric array (
valarray
)
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Pseudo-random number generation | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Bit manipulation (C++20) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Saturation arithmetic (C++26) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Factor operations | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Interpolations | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Generic numeric operations | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| C-style checked integer arithmetic | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
ヘッダーで定義
<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
関連項目
|
(C++23)
|
範囲を開始値からの連続的な増分値で埋める
(アルゴリズム関数オブジェクト) |
|
指定された値を範囲内の全要素にコピー代入する
(関数テンプレート) |
|
|
(C++20)
|
範囲の要素に特定の値を代入する
(アルゴリズム関数オブジェクト) |
|
連続する関数呼び出しの結果を範囲内の全要素に代入する
(関数テンプレート) |
|
|
(C++20)
|
関数の結果を範囲に保存する
(アルゴリズム関数オブジェクト) |
|
(C++20)
|
初期値を繰り返しインクリメントして生成されるシーケンスからなる
view
(クラステンプレート) (カスタマイゼーションポイントオブジェクト) |
適合されたシーケンスの各要素を、要素の位置と値の両方を含むタプルにマッピングする
view
(クラステンプレート) (レンジアダプタオブジェクト) |