[ Web Proxy ]
URL:
Viewing: https://en.cppreference.com/cpp/algorithm/ranges/fill_n [Back]  [Original]

std::ranges::fill_n - cppreference.com
cppreference.com
Namespaces
Variants

std::ranges::fill_n

From cppreference.com
 
 
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Constrained algorithms, e.g. ranges::copy, ranges::sort, ...
Non-modifying sequence operations    
Batch operations
(C++17)
Search operations
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
(C++11)    

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


 
Constrained algorithms
All names in this menu belong to namespace std::ranges
Non-modifying sequence operations
Fold operations (Helper templates)
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
Specialized <memory> algorithms
Return types
 
Defined in header <algorithm>
Call signature
template< class T, std::output_iterator<const T&> O >
constexpr O fill_n( O first, std::iter_difference_t<O> count, const T& value );
(1) (since C++20)
(until C++26)
template< class O, class T = std::iter_value_t<O> >
    requires std::output_iterator<O, const T&>
constexpr O fill_n( O first, std::iter_difference_t<O> count, const T& value );
(since C++26)
template< /*execution-policy*/ Ep,
          std::random_access_iterator O, class T = std::iter_value_t<O> >
    requires std::indirectly_writable<O, const T&>
O fill_n( Ep&& policy, O first, iter_difference_t<O> count, const T& value );
(2) (since C++26)

For the definition of /*execution-policy*/, see this page.

1) If count is positive, assigns value to all elements in the target range [firstranges::next(first, count)). Otherwise does nothing.
2) Same as (1), but executed according to policy.

The function-like entities described on this page are algorithm function objects (informally known as niebloids), that is:

Parameters

first - the beginning of the target range
count - number of elements to modify
value - the value to be assigned
policy - the execution policy to use

Return value

The past-the-end iterator of the target range, or first if count is non-positive.

Complexity

Exactly \(\scriptsize \max(\mathtt{count},0)\)max(count,0) assignments.

Exceptions

2) During the execution process:
  • If the temporary memory resources required for parallelization are not available, std::bad_alloc is thrown.
  • If an uncaught exception is thrown while accessing objects via an algorithm argument, the behavior is determined by the execution policy (for standard policies, std::terminate is invoked).

Notes

Feature-test macro Value Std Feature
__cpp_lib_algorithm_default_value_type 202403 (C++26) List-initialization for algorithms

Possible implementation

struct fill_n_fn
{
    template<class O, class T = std::iter_value_t<O>>
        requires std::output_iterator<O, const T&>
    constexpr O operator()(O first, std::iter_difference_t<O> count,
                           const T& value) const
    {
        for (std::iter_difference_t<O> i {}; i != count; ++first, ++i)
            *first = value;
        return first;
    }
};

inline constexpr fill_n_fn fill_n{};

Example

#include <algorithm>
#include <complex>
#include <iostream>
#include <string>
#include <vector>

void println(const auto& v)
{
    for (const auto& elem : v)
        std::cout << ' ' << elem;
    std::cout << '\n';
}

int main()
{
    constexpr auto n{8};
    
    std::vector<std::string> v(n, "");
    println(v);
    
    std::ranges::fill_n(v.begin(), n, "");
    println(v);
    
    std::vector<std::complex<double>> nums{{1, 3}, {2, 2}, {4, 8}};
    println(nums);
    #ifdef __cpp_lib_algorithm_default_value_type
        std::ranges::fill_n(nums.begin(), 2, {4, 2});
    #else
        std::ranges::fill_n(nums.begin(), 2, std::complex<double>{4, 2});
    #endif
    println(nums);
}

Output:

        
        
 (1,3) (2,2) (4,8)
 (4,2) (4,2) (4,8)

See also

copy-assigns the given value to N elements in a range
(function template) [edit]
assigns a range of elements a certain value
(algorithm function object)[edit]
copies a number of elements to a new location
(algorithm function object)[edit]
saves the result of a function in a range
(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]

Web Proxy Viewer  |  New URL  |  Original Page