Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::ranges::generate

      From cppreference.com
      <cpp‎ |algorithm‎ |ranges
       
       
      Algorithm library
      Constrained algorithms and algorithms on ranges(C++20)
      Constrained algorithms, e.g.ranges::copy,ranges::sort, ...
      Execution policies(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
      (C++11)
      (C++17)
      Lexicographical comparison operations
      Permutation operations
      C library
      Numeric operations
      Operations on uninitialized memory
       
      Constrained algorithms
      All names in this menu belong to namespacestd::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
       
      Defined in header<algorithm>
      Call signature
      template<std::input_or_output_iterator O,std::sentinel_for<O> S,

               std::copy_constructible F>
      requiresstd::invocable<F&>&&std::indirectly_writable<O,std::invoke_result_t<F&>>
      constexpr O

          generate( O first, S last, F gen);
      (1)(since C++20)
      template<class R,std::copy_constructible F>

      requiresstd::invocable<F&>&&ranges::output_range<R,std::invoke_result_t<F&>>
      constexprranges::borrowed_iterator_t<R>

          generate( R&& r, F gen);
      (2)(since C++20)
      1) Assigns the result ofsuccessive invocations of the function objectgen to each element in the range[firstlast).
      2) Same as(1), but usesr as the range, as if usingranges::begin(r) asfirst andranges::end(r) aslast.

      The function-like entities described on this page arealgorithm function objects (informally known asniebloids), that is:

      Contents

      [edit]Parameters

      first, last - the iterator-sentinel pair defining therange of elements to modify
      r - the range of elements to modify
      gen - the generator function object

      [edit]Return value

      An output iterator that compares equal tolast.

      [edit]Complexity

      Exactlyranges::distance(first, last) invocations ofgen() and assignments.

      [edit]Possible implementation

      struct generate_fn{template<std::input_or_output_iterator O,std::sentinel_for<O> S,std::copy_constructible F>    requiresstd::invocable<F&>&&std::indirectly_writable<O,std::invoke_result_t<F&>>constexpr O operator()(O first, S last, F gen)const{for(; first!= last;*first=std::invoke(gen),++first){}return first;} template<class R,std::copy_constructible F>    requiresstd::invocable<F&>&&ranges::output_range<R,std::invoke_result_t<F&>>constexprranges::borrowed_iterator_t<R> operator()(R&& r, F gen)const{return(*this)(ranges::begin(r),ranges::end(r), std::move(gen));}}; inlineconstexpr generate_fn generate{};

      [edit]Example

      Run this code
      #include <algorithm>#include <array>#include <iostream>#include <random>#include <string_view> auto dice(){staticstd::uniform_int_distribution<int> distr{1,6};staticstd::random_device device;staticstd::mt19937 engine{device()};return distr(engine);} void iota(auto& r,int init){    std::ranges::generate(r,[init] mutable{return init++;});} void print(std::string_view comment,constauto& v){for(std::cout<< comment;int i: v)std::cout<< i<<' ';std::cout<<'\n';} int main(){std::array<int,8> v;     std::ranges::generate(v.begin(), v.end(), dice);    print("dice: ", v);    std::ranges::generate(v, dice);    print("dice: ", v);     iota(v,1);    print("iota: ", v);}

      Possible output:

      dice: 4 3 1 6 6 4 5 5dice: 4 2 5 3 6 2 6 2iota: 1 2 3 4 5 6 7 8

      [edit]See also

      saves the result of N applications of a function
      (algorithm function object)[edit]
      assigns a range of elements a certain value
      (algorithm function object)[edit]
      assigns a value to a number of elements
      (algorithm function object)[edit]
      applies a function to a range of elements
      (algorithm function object)[edit]
      fills a range with random numbers from a uniform random bit generator
      (algorithm function object)[edit]
      assigns the results of successive function calls to every element in a range
      (function template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/algorithm/ranges/generate&oldid=180505"

      [8]ページ先頭

      ©2009-2025 Movatter.jp