Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/algorithm/ranges/generate n"

From cppreference.com
< cpp‎ | algorithm‎ | ranges
m (Parameters: '.')
m (See also: +generate_random.)
 
(3 intermediate revisions by 3 users not shown)
Line 2: Line 2:
 
{{cpp/algorithm/ranges/navbar}}
 
{{cpp/algorithm/ranges/navbar}}
 
{{dcl begin}}
 
{{dcl begin}}
{{dcl header | algorithm}}
+
{{dcl header|algorithm}}
{{dcl h | Call signature}}
+
{{dcl h|Call signature}}
{{dcl | num=1 | since=c++20 |1=
+
{{dcl|since=c++20|1=
template<std::input_or_output_iterator O, std::copy_constructible F>
+
template< std::input_or_output_iterator O, std::copy_constructible F >
  requires std::invocable<F&> && std::indirectly_writable<O, std::invoke_result_t<F&>>
+
requires std::invocable<F&> && std::indirectly_writable<O, std::invoke_result_t<F&>>
    constexpr O generate_n( O first, std::iter_difference_t<O> n, F gen );
+
constexpr O
 +
    generate_n( O first, std::iter_difference_t<O> n, F gen );
 
}}
 
}}
 
{{dcl end}}
 
{{dcl end}}
  
Assigns the result of ''successive'' invocations of the function object {{tt|gen}} to each element in the range {{tt|[first, first + n)}}, if {{c|0 < n}}. Does nothing otherwise.
+
Assigns the result of ''successive'' invocations of the function object {{c|gen}} to each element in the range {{range|first|first + n}}, if {{c|0 < n}}. Does nothing otherwise.
  
 
{{cpp/ranges/niebloid}}
 
{{cpp/ranges/niebloid}}
Line 17: Line 18:
 
===Parameters===
 
===Parameters===
 
{{par begin}}
 
{{par begin}}
{{par | first | the beginning of the range of elements to modify}}
+
{{par|first|the beginning of the range of elements to modify}}
{{par | n | number of elements to modify}}
+
{{par|n|number of elements to modify}}
{{par | gen | the generator function object.}}
+
{{par|gen|the generator function object.}}
 
{{par end}}
 
{{par end}}
  
 
===Return value===
 
===Return value===
Iterator one past the last element assigned if {{c|0 < count}}, {{tt|first}} otherwise.
+
Iterator one past the last element assigned if {{c|0 < count}}, {{c|first}} otherwise.
  
 
===Complexity===
 
===Complexity===
Exactly {{c|n}} invocations of {{tt|gen()}} and assignments.
+
Exactly {{c|n}} invocations of {{c|gen()}} and assignments.
  
 
===Possible implementation===
 
===Possible implementation===
{{eq fun | 1=
+
{{eq fun|1=
struct generate_n_fn {
+
struct generate_n_fn
  template<std::input_or_output_iterator O, std::copy_constructible F>
+
{
 +
    template<std::input_or_output_iterator O, std::copy_constructible F>
 
     requires std::invocable<F&> && std::indirectly_writable<O, std::invoke_result_t<F&>>
 
     requires std::invocable<F&> && std::indirectly_writable<O, std::invoke_result_t<F&>>
      constexpr O operator()( O first, std::iter_difference_t<O> n, F gen ) const {
+
    constexpr O operator()(O first, std::iter_difference_t<O> n, F gen) const
         for (; n-- > 0; *first = std::invoke(gen), ++first);
+
    {
 +
         for (; n-- > 0; *first = std::invoke(gen), ++first)
 +
        {}
 
         return first;
 
         return first;
      }
+
    }
 
};
 
};
  
inline constexpr generate_n_fn generate_n{};
+
inline constexpr generate_n_fn generate_n {};
 
}}
 
}}
  
Line 50: Line 54:
 
#include <string_view>
 
#include <string_view>
  
auto dice() {
+
auto dice()
     static std::uniform_int_distribution<int> distr{1, 6};
+
{
 +
     static std::uniform_int_distribution<int> distr {1, 6};
 
     static std::random_device engine;
 
     static std::random_device engine;
     static std::mt19937 noise{engine()};
+
     static std::mt19937 noise {engine()};
 
     return distr(noise);
 
     return distr(noise);
 
}
 
}
  
void print(const auto& v, std::string_view comment) {
+
void print(const auto& v, std::string_view comment)
     for (int i : v) { std::cout << i << ' '; }
+
{
     std::cout << "(" << comment << ")\n";
+
     for (int i : v)
 +
        std::cout << i << ' ';
 +
     std::cout << '(' << comment << ")\n";
 
}
 
}
  
Line 69: Line 76:
 
     print(v, "dice");
 
     print(v, "dice");
  
     std::ranges::generate_n(v.begin(), v.size(), [n{0}] () mutable { return n++; });
+
     std::ranges::generate_n(v.begin(), v.size(), [n {0}] mutable { return n++; });
 
     // same effect as std::iota(v.begin(), v.end(), 0);
 
     // same effect as std::iota(v.begin(), v.end(), 0);
 
     print(v, "iota");
 
     print(v, "iota");
 
}
 
}
| p=true
+
|p=true
| output=
+
|output=
 
5 5 2 2 6 6 3 5 (dice)
 
5 5 2 2 6 6 3 5 (dice)
 
0 1 2 3 4 5 6 7 (iota)
 
0 1 2 3 4 5 6 7 (iota)
Line 81: Line 88:
 
===See also===
 
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/algorithm/ranges/dsc generate}}
+
{{dsc inc|cpp/algorithm/ranges/dsc generate}}
{{dsc inc | cpp/algorithm/ranges/dsc fill}}
+
{{dsc inc|cpp/numeric/random/ranges/dsc generate_random}}
{{dsc inc | cpp/algorithm/ranges/dsc fill_n}}
+
{{dsc inc|cpp/algorithm/ranges/dsc fill}}
{{dsc inc | cpp/algorithm/ranges/dsc transform}}
+
{{dsc inc|cpp/algorithm/ranges/dsc fill_n}}
{{dsc inc | cpp/algorithm/dsc generate}}
+
{{dsc inc|cpp/algorithm/ranges/dsc transform}}
 +
{{dsc inc|cpp/algorithm/dsc generate_n}}
 
{{dsc end}}
 
{{dsc end}}
  
 
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}
 
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}

Latest revision as of 14:55, 17 April 2024

 
 
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Constrained algorithms, e.g. ranges::copy, ranges::sort, ...
Execution policies (C++17)
Non-modifying sequence operations
Batch operations
(C++17)
Search operations
(C++11)                (C++11)(C++11)

Modifying sequence operations
Copy operations
(C++11)
(C++11)
Swap operations
Transformation operations
Generation operations
Removing operations
Order-changing operations
(until C++17)(C++11)
(C++20)(C++20)
Sampling operations
(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 namespace 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
Numeric operations
(C++23)            
Operations on uninitialized storage
Return types
 
Defined in header <algorithm>
Call signature
template< std::input_or_output_iterator O, std::copy_constructible F >

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

    generate_n( O first, std::iter_difference_t<O> n, F gen );
(since C++20)

Assigns the result of successive invocations of the function object gen to each element in the range [firstfirst + n), if 0 < n. Does nothing otherwise.

The function-like entities described on this page are niebloids, that is:

In practice, they may be implemented as function objects, or with special compiler extensions.

Contents

[edit] Parameters

first - the beginning of the range of elements to modify
n - number of elements to modify
gen - the generator function object.

[edit] Return value

Iterator one past the last element assigned if 0 < count, first otherwise.

[edit] Complexity

Exactly n invocations of gen() and assignments.

[edit] Possible implementation

struct generate_n_fn
{
    template<std::input_or_output_iterator O, std::copy_constructible F>
    requires std::invocable<F&> && std::indirectly_writable<O, std::invoke_result_t<F&>>
    constexpr O operator()(O first, std::iter_difference_t<O> n, F gen) const
    {
        for (; n-- > 0; *first = std::invoke(gen), ++first)
        {}
        return first;
    }
};
 
inline constexpr generate_n_fn generate_n {};

[edit] Example

#include <algorithm>
#include <array>
#include <iostream>
#include <random>
#include <string_view>
 
auto dice()
{
    static std::uniform_int_distribution<int> distr {1, 6};
    static std::random_device engine;
    static std::mt19937 noise {engine()};
    return distr(noise);
}
 
void print(const auto& v, std::string_view comment)
{
    for (int i : v)
        std::cout << i << ' ';
    std::cout << '(' << comment << ")\n";
}
 
int main()
{
    std::array<int, 8> v;
 
    std::ranges::generate_n(v.begin(), v.size(), dice);
    print(v, "dice");
 
    std::ranges::generate_n(v.begin(), v.size(), [n {0}] mutable { return n++; });
    // same effect as std::iota(v.begin(), v.end(), 0);
    print(v, "iota");
}

Possible output:

5 5 2 2 6 6 3 5 (dice)
0 1 2 3 4 5 6 7 (iota)

[edit] See also

saves the result of a function in a range
(niebloid)[edit]
fills a range with random numbers from a uniform random bit generator
(niebloid)[edit]
assigns a range of elements a certain value
(niebloid)[edit]
assigns a value to a number of elements
(niebloid)[edit]
applies a function to a range of elements
(niebloid)[edit]
assigns the results of successive function calls to N elements in a range
(function template) [edit]