Spec-Zone.ru › C++

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

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

#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

fill
copy-assigns the given value to every element in a range
(function template & algorithm function object)
ranges::fill
(C++20)
fill_n
copy-assigns the given value to N elements in a range
(function template & algorithm function object)
ranges::fill_n
(C++20)
ranges::fill
(C++20)
assigns a range of elements a certain value
(algorithm function object)

© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/cpp/container/array/fill

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API