Namespaces
Variants

std::ranges:: fill

From cppreference.net
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Constrained algorithms, e.g. ranges::copy , ranges::sort , ...
Execution policies (C++17)
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
Sorting operations
Binary search operations
(on partitioned ranges)
Set operations (on sorted ranges)
Merge operations (on sorted ranges)
Heap operations
Minimum/maximum operations
Lexicographical comparison operations
Permutation operations
C library
Numeric operations
Operations on uninitialized memory
Constrained algorithms
All names in this menu belong to namespace std::ranges
Non-modifying sequence operations
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
Fold operations
Operations on uninitialized storage
Return types
Definiert im Header <algorithm>
Aufrufsignatur
(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 ) ;
(seit C++20)
(bis C++26)
template < class O, std:: sentinel_for < O > S, class T = std:: iter_value_t < O > >

requires std:: output_iterator < O, const T & >

constexpr O fill ( O first, S last, const T & value ) ;
(seit C++26)
(2)
template < class T, ranges:: output_range < const T & > R >
constexpr ranges:: borrowed_iterator_t < R > fill ( R && r, const T & value ) ;
(seit C++20)
(bis C++26)
template < class R, class T = ranges:: range_value_t < R > >

requires ranges:: output_range < R, const T & >

constexpr ranges:: borrowed_iterator_t < R > fill ( R && r, const T & value ) ;
(seit C++26)
1) Weist den Elementen im Bereich [ first , last ) den gegebenen value zu.
2) Gleich wie (1) , verwendet jedoch r als Quellbereich, als ob ranges:: begin ( r ) als first und ranges:: end ( r ) als last verwendet würde.

Die auf dieser Seite beschriebenen funktionsähnlichen Entitäten sind Algorithm Function Objects (informell bekannt als Niebloids ), das heißt:

Inhaltsverzeichnis

Parameter

first, last - das Iterator-Sentinel-Paar, das den Bereich der zu ändernden Elemente definiert
r - der Bereich der zu ändernden Elemente
value - der zuzuweisende Wert

Rückgabewert

Ein Ausgabeiterator, der gleich last vergleicht.

Komplexität

Genau last - first Zuweisungen.

Mögliche Implementierung

struct fill_fn
{
    template<class O, std::sentinel_for<O> S, class T = std::iter_value_t<O>>
    requires std::output_iterator<O, const T&>
    constexpr O operator()(O first, S last, const T& value) const
    {
        while (first != last)
            *first++ = value;
        return first;
    }
    template<class R, class T = ranges::range_value_t<R>>
    requires ranges::output_range<R, const T&>
    constexpr ranges::borrowed_iterator_t<R> operator()(R&& r, const T& value) const
    {
        return (*this)(ranges::begin(r), ranges::end(r), value);
    }
};
inline constexpr fill_fn fill;

Hinweise

Feature-Test Makro Wert Std Feature
__cpp_lib_algorithm_default_value_type 202403 (C++26) Listeninitialisierung für Algorithmen ( 1,2 )

Beispiel

#include <algorithm>
#include <complex>
#include <iostream>
#include <vector>
void println(const auto& seq)
{
    for (const auto& e : seq)
        std::cout << e << ' ';
    std::cout << '\n';
}
int main()
{
    std::vector<int> v{0, 1, 2, 3, 4, 5};
    // Alle Elemente auf -1 setzen mit Überladung (1)
    std::ranges::fill(v.begin(), v.end(), -1);
    println(v);
    // Alle Elemente auf 10 setzen mit Überladung (2)
    std::ranges::fill(v, 10);
    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(nums, {4, 2}); // T wird abgeleitet
    #else
        std::ranges::fill(nums, std::complex<double>{4, 2});
    #endif
    println(nums);
}

Ausgabe:

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

Siehe auch

weist einer Anzahl von Elementen einen Wert zu
(Algorithmus-Funktionsobjekt)
kopiert einen Elementbereich an einen neuen Speicherort
(Algorithmus-Funktionsobjekt)
speichert das Ergebnis einer Funktion in einem Bereich
(Algorithmus-Funktionsobjekt)
wendet eine Funktion auf einen Elementbereich an
(Algorithmus-Funktionsobjekt)
füllt einen Bereich mit Zufallszahlen aus einem gleichverteilten Zufallsbitgenerator
(Algorithmus-Funktionsobjekt)
weist jedem Element in einem Bereich den gegebenen Wert per Copy-Zuweisung zu
(Funktionstemplate)