55
66``` cpp
77namespace std {
8- template <class OutputIterator, class Size, class T>
8+ template <class OutputIterator,
9+ class Size,
10+ class T>
911 void
1012 fill_n(OutputIterator first,
1113 Size n,
1214 const T& value); // (1) C++03
13-
14- template <class OutputIterator, class Size, class T>
15+ template <class OutputIterator,
16+ class Size,
17+ class T>
1518 OutputIterator
1619 fill_n(OutputIterator first,
1720 Size n,
1821 const T& value); // (1) C++11
19-
20- template <class OutputIterator, class Size, class T>
22+ template <class OutputIterator,
23+ class Size,
24+ class T>
2125 constexpr OutputIterator
2226 fill_n(OutputIterator first,
2327 Size n,
2428 const T& value); // (1) C++20
29+ template <class OutputIterator,
30+ class Size,
31+ class T = typename iterator_traits<OutputIterator >::value_type>
32+ constexpr OutputIterator
33+ fill_n(OutputIterator first,
34+ Size n,
35+ const T& value); // (1) C++26
2536
26- template <class ExecutionPolicy, class ForwardIterator,
27- class Size, class T>
37+ template <class ExecutionPolicy,
38+ class ForwardIterator,
39+ class Size,
40+ class T>
2841 ForwardIterator
2942 fill_n(ExecutionPolicy&& exec,
3043 ForwardIterator first,
3144 Size n,
3245 const T& value); // (2) C++17
46+ template <class ExecutionPolicy,
47+ class ForwardIterator,
48+ class Size,
49+ class T = typename iterator_traits<ForwardIterator >::value_type>
50+ ForwardIterator
51+ fill_n(ExecutionPolicy&& exec,
52+ ForwardIterator first,
53+ Size n,
54+ const T& value); // (2) C++26
3355}
3456```
3557
3658## 概要
3759イテレータ範囲`[first, first + n)`のすべての要素に指定された値を書き込む。
3860
3961
40- ## 要件
62+ ## 適格要件
4163- `value` は `output iterator` へ書き込み可能でなければならない。
4264- `Size` は `integral type` に変換可能でなければならない。
4365
@@ -57,24 +79,69 @@ namespace std {
5779`n` が 1 以上の場合は `n` 回、そうでない場合は 0 回の代入を行う。
5880
5981
82+ ## 備考
83+ - (1), (2) :
84+ - C++26 : `value`パラメータとして波カッコ初期化`{}`を受け付ける
85+ ```cpp
86+ std::vector<T> v;
87+ std::fill_n(v.begin(), n, {a, b});
88+ ```
89+
90+
6091## 例
92+ ### 基本的な使い方
6193```cpp example
6294#include <algorithm>
6395#include <iostream>
6496#include <iterator>
6597
6698int main() {
67- // 3 を出力しまくる
99+ // 値3を10個出力する
68100 std::fill_n(std::ostream_iterator<int>(std::cout, ","), 10, 3);
69101}
70102```
71103* std::fill_n[ color ff0000]
72104
73- ### 出力
105+ #### 出力
74106```
751073,3,3,3,3,3,3,3,3,3,
76108```
77109
110+ ### 波カッコ初期化を入力として使用する (C++26)
111+ ``` cpp example
112+ #include < algorithm>
113+ #include < iostream>
114+ #include < vector>
115+
116+ struct Point {
117+ int x;
118+ int y;
119+
120+ bool operator==(const Point& other) const = default;
121+ };
122+
123+ int main() {
124+ std::vector<Point > v(5);
125+
126+ // 先頭3個の要素を値{1, 2}で埋める
127+ std::fill_n(v.begin(), 3, {1, 2});
128+
129+ for (const Point& p : v) {
130+ std::cout << p.x << "," << p.y << std::endl;
131+ }
132+ }
133+ ```
134+ * std::fill_n[color ff0000]
135+
136+ #### 出力
137+ ```
138+ 1,2
139+ 1,2
140+ 1,2
141+ 0,0
142+ 0,0
143+ ```
144+
78145
79146## 実装例
80147```cpp
@@ -107,3 +174,5 @@ fill_n(OutputIterator first, Size n, const T& value) {
107174 戻り値が追加されるきっかけとなったレポート
108175- [ P0202R3 Add Constexpr Modifiers to Functions in ` <algorithm> ` and ` <utility> ` Headers] ( http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0202r3.html )
109176- [ P0467R2 Iterator Concerns for Parallel Algorithms] ( http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0467r2.html )
177+ - [ P2248R8 Enabling list-initialization for algorithms] ( https://open-std.org/jtc1/sc22/wg21/docs/papers/2024/p2248r8.html )
178+ - C++26で波カッコ初期化 (リスト初期化) に対応した
0 commit comments