std::ranges::fill() algorithm
- od C++20
- Simplified
- Detailed
// (1)
template<
class T,
std::output_iterator<const T&> O,
std::sentinel_for<O> S
>
constexpr O fill( O first, S last, const T& value );
// (2)
template<
class T,
ranges::output_range<const T&> R
>
constexpr ranges::borrowed_iterator_t<R> fill( R&& r, const T& value );
The type of arguments are generic and have the following constraints:
T- (none)O-std::output_iterator<const T&>S-std::sentinel_for<O>- (2) -
R-std::ranges::output_range<const T&>
// (1)
template<
class T,
std::output_iterator<const T&> O,
std::sentinel_for<O> S
>
constexpr O fill( O first, S last, const T& value );
// (2)
template<
class T,
ranges::output_range<const T&> R
>
constexpr ranges::borrowed_iterator_t<R> fill( R&& r, const T& value );
- (1) Assigns the given value to the elements in the range [
first;last). - (2) Same as (1), but uses
ras the source range, as if usingranges::begin(r)asfirstandranges::end(r)aslast.
The function-like entities described on this page are niebloids.
Parameters
first last | The range of elements to examine. |
r | The range of elements to examine. |
value | The value to assign. |
Return value
An output iterator that compares equal to last.
Complexity
Exactly last - first assignments.
Exceptions
(none)
Possible implementation
ranges::fill
Examples
The following code uses ranges::fill to set all elements of std::vector<int> first to -1, then to 10.
Main.cpp
#include <algorithm>
#include <iostream>
#include <vector>
void println(std::vector<int> const& vi)
{
for (int e : vi)
std::cout << e << ' ';
std::cout << '\n';
}
int main()
{
std::vector<int> v {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
std::ranges::fill(v.begin(), v.end(), -1);
println(v);
std::ranges::fill(v, 10);
println(v);
}
Output
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1
10 10 10 10 10 10 10 10 10 10
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.