最終更新日時(UTC):
が更新

履歴 編集

function template
<algorithm>

std::ranges::generate_n(C++20)

namespace std::ranges {
  template <input_or_output_iterator O,
            copy_constructible F>
    requires invocable<F&> && indirectly_writable<O, invoke_result_t<F&>>
  constexpr O
    generate_n(O first,
               iter_difference_t<O> n,
               F gen);                  // (1) C++20

  template <execution-policy Ep,
            random_access_iterator O,
            copy_constructible F>
    requires invocable<F&> && indirectly_writable<O, invoke_result_t<F&>>
  O generate_n(Ep&& exec,
               O first,
               iter_difference_t<O> n,
               F gen);                  // (2) C++26
}

概要

出力の範囲へ関数の結果を n 個書き込む。

  • (1): イテレータ範囲を指定する
  • (2): (1)の並列アルゴリズム版。実行ポリシーを指定する

効果

n が 1 以上の場合、[first,last) のそれぞれのイテレータについて関数オブジェクト gen を呼び出し、その戻り値を代入する。

そうでない場合、何もしない。

戻り値

n が 1 以上の場合、first + n が返される。
そうでない場合、first が返される。

計算量

n が 1 以上の場合、n 回の gen の呼び出しと代入が行われる。

そうでない場合、何もしない。

基本的な使い方

#include <algorithm>
#include <iostream>
#include <iterator>

int main() {
  // 2 の累乗の値を生成して出力する
  int n = 1;
  std::ranges::generate_n(std::ostream_iterator<int>(std::cout, ","), 10, [&n]{ auto t = n; n *= 2; return t; });
}

出力

1,2,4,8,16,32,64,128,256,512,

並列アルゴリズムの例 (C++26)

#include <algorithm>
#include <execution>
#include <iostream>
#include <vector>
#include <atomic>

int main() {
  std::vector<int> v(5);

  // 並列に値を生成して書き込む
  std::atomic<int> counter{0};
  std::ranges::generate_n(std::execution::par, v.begin(), 5,
                          [&counter] { return counter++; });

  for (int x : v) {
    std::cout << x << ' ';
  }
  std::cout << std::endl;
}

出力例

0 1 2 3 4

バージョン

言語

  • C++20

処理系

参照