std::ranges:: iota, std::ranges:: iota_result
std::ranges
| Non-modifying sequence operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Modifying sequence operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Partitioning operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Sorting operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Binary search operations (on sorted ranges) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Set operations (on sorted ranges) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Heap operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Minimum/maximum operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Permutation operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Fold operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Operations on uninitialized storage | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Return types | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
<
std::
input_or_output_iterator
O,
std::
sentinel_for
<
O
>
S,
std::
weakly_incrementable
T
>
|
(1) | (C++23以降) |
|
template
<
std::
weakly_incrementable
T,
ranges::
output_range
<
const
T
&
>
R
>
constexpr
iota_result
<
ranges::
borrowed_iterator_t
<
R
>
, T
>
|
(2) | (C++23以降) |
|
ヘルパー型
|
||
|
template
<
class
O,
class
T
>
using iota_result = ranges:: out_value_result < O, T > ; |
(3) | (C++23以降) |
範囲
[
first
,
last
)
を、
value
から始まり、
++
value
を繰り返し評価することで得られる連続的に増加する値で埋めます。
同等の操作:
*(first) = value; *(first + 1) = ++value; *(first + 2) = ++value; *(first + 3) = ++value; ...
タグ内のC++コードは翻訳対象外です。表示されているコード部分は全てC++コードであるため、翻訳は行われていません)
目次 |
パラメータ
| first, last | - | 要素を順次増加する値で埋める 範囲 を定義するイテレータ-センチネルペア。 value から開始する |
| value | - | 格納する初期値。 ++ value 式は適切に形成されなければならない |
戻り値
{ last, value + ranges:: distance ( first, last ) }
計算量
正確に last - first 回のインクリメントと代入が行われます。
実装例
struct iota_fn { template<std::input_or_output_iterator O, std::sentinel_for<O> S, std::weakly_incrementable T> requires std::indirectly_writable<O, const T&> constexpr iota_result<O, T> operator()(O first, S last, T value) const { while (first != last) { *first = as_const(value); ++first; ++value; } return {std::move(first), std::move(value)}; } template<std::weakly_incrementable T, std::ranges::output_range<const T&> R> constexpr iota_result<std::ranges::borrowed_iterator_t<R>, T> operator()(R&& r, T value) const { return (*this)(std::ranges::begin(r), std::ranges::end(r), std::move(value)); } }; inline constexpr iota_fn iota; |
注記
この関数は、プログラミング言語 APL の整数関数 ⍳ に因んで命名されています。
| 機能テスト マクロ | 値 | 標準 | 機能 |
|---|---|---|---|
__cpp_lib_ranges_iota
|
202202L
|
(C++23) |
std::ranges::iota
|
例
vector のイテレータの( std:: vector < std:: list < T > :: iterator > )をプロキシとして使用して std::list の要素をシャッフルします。これは ranges::shuffle を std::list に直接適用できないためです。
#include <algorithm> #include <functional> #include <iostream> #include <list> #include <numeric> #include <random> #include <vector> template <typename Proj = std::identity> void println(auto comment, std::ranges::input_range auto&& range, Proj proj = {}) { for (std::cout << comment; auto const &element : range) std::cout << proj(element) << ' '; std::cout << '\n'; } int main() { std::list<int> list(8); // リストを昇順の値で埋める: 0, 1, 2, ..., 7 std::ranges::iota(list, 0); println("List: ", list); // イテレータのvector(Exampleのコメントを参照) std::vector<std::list<int>::iterator> vec(list.size()); // 連続するリスト要素へのイテレータで埋める std::ranges::iota(vec.begin(), vec.end(), list.begin()); std::ranges::shuffle(vec, std::mt19937 {std::random_device {}()}); println("List viewed via vector: ", vec, [](auto it) { return *it; }); }
出力例:
List: 0 1 2 3 4 5 6 7 List viewed via vector: 5 7 6 0 1 3 4 2
関連項目
|
指定された値を範囲内のすべての要素にコピー代入する
(関数テンプレート) |
|
|
(C++20)
|
範囲の要素に特定の値を代入する
(アルゴリズム関数オブジェクト) |
|
連続する関数呼び出しの結果を範囲内のすべての要素に代入する
(関数テンプレート) |
|
|
(C++20)
|
関数の結果を範囲に保存する
(アルゴリズム関数オブジェクト) |
|
(C++20)
|
初期値を繰り返しインクリメントして生成されるシーケンスからなる
view
(クラステンプレート) (カスタマイゼーションポイントオブジェクト) |
|
(C++11)
|
範囲を開始値からの連続的な増分値で埋める
(関数テンプレート) |