C++ <array> fill function

C++ container class <array>


fillis used in array toFill all elementsA function that can quickly set all elements to the same value.

fillis a member function of the container class, used toSet all elements in the container to the specified value.This is an O(n) operation that requires iterating through all elements.

fillIs a shortcut for initializing or resetting the array.

Word Definitions: fillMeans "fill", i.e., setting all elements to a specified value.


Basic syntax and parameters

fillIs a member function of the container class, requiring a specified fill value.

Syntax format

void fill(const T& value);

Parameter description

  • Parameter: value
    • Type: same as the container element type
    • Description: The value to fill into all elements.

Function description

  • Return Value: void(No return value).
  • Effect: Set every element in the container tovalue。
  • Note: fillmodifies all elements in the container.

Example

Let us thoroughly master through a series of examples.fillUsage.

Example 1: Basic usage - filling with the same value

Example

#include <iostream>
#include <array>

int main() {
    std::array<int, 5> numbers = {1, 2, 3, 4, 5};

    std::cout << "Before fill: ";
    for(int n : numbers) {
        std::cout << n << " ";
    }
    std::cout << std::endl;

    // Fill all elements with 0
    numbers.fill(0);

    std::cout << "After fill: ";
    for(int n : numbers) {
        std::cout << n << " ";
    }
    std::cout << std::endl;

    return 0;
}

Expected run result:

填充前: 1 2 3 4 5
填充后: 0 0 0 0 0

Code analysis:

  1. numbers.fill(0)Set all 5 elements to 0.
  2. This is an O(n) operation, traversing all elements and assigning values.

Example 2: Initializing to a specific value

Usagefillcan quickly initialize an array.

Example

#include <iostream>
#include <array>
#include <string>

int main() {
    // Create and initialize to -1
    std::array<int, 10> scores;
    scores.fill(-1);

    std::cout << "Initialized to -1: ";
    for(int s : scores) {
        std::cout << s << " ";
    }
    std::cout << std::endl;

    // Create and initialize to empty string
    std::array<std::string, 3> names;
    names.fill("Unknown");

    std::cout << "Initialized to Unknown: ";
    for(const auto& n : names) {
        std::cout << n << " ";
    }
    std::cout << std::endl;

    return 0;
}

Expected run result:

初始化为 -1: -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
初始化为 Unknown: Unknown Unknown Unknown

Code analysis:

  • fillCan be used to quickly initialize an array of any type.
  • For integers, 0 or -1 is commonly used; for strings, an empty string or specific placeholder is commonly used.
  • Example 3: Resetting the array

    fillcan be used to reset the array to its initial state.

    Example

    #include <iostream>
    #include <array>

    int main() {
        std::array<int, 5> buffer;

        // Initialize to 1-5
        for(int i = 0; i < 5; ++i) {
            buffer[i] = i + 1;
        }

        std::cout << "Initial data: ";
        for(int n : buffer) std::cout << n << " ";
        std::cout << std::endl;

        // Simulate resetting after using buffer
        buffer.fill(0);

        std::cout << "After reset: ";
        for(int n : buffer) std::cout << n << " ";
        std::cout << std::endl;

        return 0;
    }

    Expected run result:

    初始数据: 1 2 3 4 5
    重置后: 0 0 0 0 0
    

    Code analysis:

    • fillCan quickly reset the array to a known state.
    • This is useful when the same buffer needs to be used multiple times.

    Example 4: Using fill with std::generate

    fillused in conjunction with STL algorithms.

    Example

    #include <iostream>
    #include <array>
    #include <algorithm>

    int main() {
        std::array<int, 10> numbers;

        // Use fill to populate initial values
        numbers.fill(1);

        std::cout << "After fill 1: ";
        for(int n : numbers) std::cout << n << " ";
        std::cout << std::endl;

        // Increment each element
        for(auto& n : numbers) {
            n *= 2;
        }

        std::cout << "After doubling: ";
        for(int n : numbers) std::cout << n << " ";
        std::cout << std::endl;

        return 0;
    }

    Code analysis:

    • fillused to set initial values.
    • Afterwards, it can be further processed with loops or algorithms.

    C++ container class <array>

    other extensions