Przejdź do głównej zawartości

std::array fill() method

// Non-const version only
constexpr void fill( const T& value );

Assigns the given value 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

#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
▄▀▄▀▄▀▄▀
▄▀▄▀▄▀▄▀
▄▀▄▀▄▀▄▀
▄▀▄▀▄▀▄▀
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::array fill() method

// Non-const version only
constexpr void fill( const T& value );

Assigns the given value 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

#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
▄▀▄▀▄▀▄▀
▄▀▄▀▄▀▄▀
▄▀▄▀▄▀▄▀
▄▀▄▀▄▀▄▀
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.