std::array<T,N>:: fill
From cppreference.net
C++
Containers library
|
(C++17)
|
||||
| Sequence | ||||
|
(C++11)
|
||||
|
(C++26)
|
||||
|
(C++26)
|
||||
|
(C++11)
|
||||
| Associative | ||||
| Unordered associative | ||||
|
(C++11)
|
||||
|
(C++11)
|
||||
|
(C++11)
|
||||
|
(C++11)
|
||||
| Adaptors | ||||
|
(C++23)
|
||||
|
(C++23)
|
||||
|
(C++23)
|
||||
|
(C++23)
|
||||
| Views | ||||
|
(C++20)
|
||||
|
(C++23)
|
||||
| Tables | ||||
| Iterator invalidation | ||||
| Member function table | ||||
| Non-member function table |
std::array
| Member types | ||||||||||||||||||||||||||
| Member functions | ||||||||||||||||||||||||||
| Non-member functions | ||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||
| Helper classes | ||||||||||||||||||||||||||
| Deduction guides (C++17) | ||||||||||||||||||||||||||
|
void
fill
(
const
T
&
value
)
;
|
(自 C++11 起)
(自 C++20 起为 constexpr) |
|
将 value 赋值给容器中的所有元素。
目录 |
参数
| value | - | 要分配给元素的值 |
返回值
(无)
复杂度
与容器大小呈线性关系。
示例
运行此代码
#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"); }
可能的输出:
▄▀▄▀▄▀▄▀ ▄▀▄▀▄▀▄▀ ▄▀▄▀▄▀▄▀ ▄▀▄▀▄▀▄▀
参见
|
将给定值复制赋值给范围内的每个元素
(函数模板) |
|
|
将给定值复制赋值给范围内的 N 个元素
(函数模板) |
|
|
(C++20)
|
为范围内的元素赋予特定值
(算法函数对象) |