std::ranges::fill_n() algorithm
- od C++20
- Simplified
- Detailed
// (1)
constexpr O fill_n( O first, std::iter_difference_t<O> n, const T& value );
The type of arguments are generic and have the following constraints:
T
- (none)O
-std::output_iterator<const T&>
// (1)
template<
class T,
std::output_iterator<const T&> O
>
constexpr O fill_n( O first, std::iter_difference_t<O> n, const T& value );
Assigns the given value to all elements in the range [first
; first + n
).
The function-like entities described on this page are niebloids.
Parameters
first | The beginning of the range of elements to modify. |
n | Number of elements to modify. |
value | The value to assign. |
Return value
An output iterator that compares equal to first + n
.
Complexity
Exactly n
assignments.
Exceptions
(none)
Possible implementation
ranges::fill_n
struct fill_n_fn
{
template<class T, std::output_iterator<const T&> O>
constexpr O operator()(O first, std::iter_difference_t<O> n, const T& value) const
{
for (std::iter_difference_t<O> i {}; i != n; ++first, ++i)
*first = value;
return first;
}
};
inline constexpr fill_n_fn fill_n {};
Examples
#include <algorithm>
#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 {010};
std::vector<std::string> v(n, "▓▓░░");
println(v);
std::ranges::fill_n(v.begin(), n, "░░▓▓");
println(v);
}
▓▓░░ ▓▓░░ ▓▓░░ ▓▓░░ ▓▓░░ ▓▓░░ ▓▓░░ ▓▓░░
░░▓▓ ░░▓▓ ░░▓▓ ░░▓▓ ░░▓▓ ░░▓▓ ░░▓▓ ░░▓▓
Hover to see the original license.