std::ranges::fill_n() algorithm
- since 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
Examples
Main.cpp
#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);
}
Output
▓▓░░ ▓▓░░ ▓▓░░ ▓▓░░ ▓▓░░ ▓▓░░ ▓▓░░ ▓▓░░
░░▓▓ ░░▓▓ ░░▓▓ ░░▓▓ ░░▓▓ ░░▓▓ ░░▓▓ ░░▓▓
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.
Hover to see the original license.