Skip to content

Search topics & cppreference

Search all study topics, concept maps, and 6,587 C++ and C reference pages.

std::array<T,N>::fill

Assigns the value to all elements in the container.

Containers
  • C++11
  • C++20
1 min readView on cppreference
NotebookLM
void fill( const T& value );
(since C++11)
(constexpr since C++20)

Assigns the value to all elements in the container.

Parameters

value - the value to assign to the elements

Return value

(none)

Complexity

Linear in the size of the container.

Example

example.cpp
#include <array>
#include <cstddef>
#include <iostream>
 
int main()
{
    constexpr std::size_t xy = 4;
 
    using Cell = std::array<unsigned char, 8>;
 
    std::array<Cell, xy * xy> board;
 
    board.fill({0xE2, 0x96, 0x84, 0xE2, 0x96, 0x80, 0, 0}); // "▄▀";
 
    for (std::size_t count{}; Cell c : board)
        std::cout << c.data() << ((++count % xy) ? "" : "\n");
}
Possible output
▄▀▄▀▄▀▄▀
▄▀▄▀▄▀▄▀
▄▀▄▀▄▀▄▀
▄▀▄▀▄▀▄▀

See also

copy-assigns the given value to every element in a range
(function template)
copy-assigns the given value to N elements in a range
(function template)
assigns a range of elements a certain value
(algorithm function object)

Content from this page on cppreference.com, licensed under CC-BY-SA 3.0 and GFDL. Modified: restyled, site navigation removed, code windows added. Offline snapshot of 9 February 2025.