Przejdź do głównej zawartości

std::ranges::fill_n() algorithm

// (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&>

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

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.

std::ranges::fill_n() algorithm

// (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&>

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

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.