GeezerVison is showing cpprefjp.github.io — std::fill_n - cpprefjp C++日本語リファレンス
最終更新日時:
2026年09月20日 18時40分00秒 (JST)
rotarymars
が更新
履歴
編集
function template
<algorithm>
std:: fill_n
namespace std {
template < class OutputIterator ,
class Size ,
class T >
void
fill_n ( OutputIterator first ,
Size n ,
const T & value ); // (1) C++98
template < class OutputIterator ,
class Size ,
class T >
OutputIterator
fill_n ( OutputIterator first ,
Size n ,
const T & value ); // (1) C++11
template < class OutputIterator ,
class Size ,
class T >
constexpr OutputIterator
fill_n ( OutputIterator first ,
Size n ,
const T & value ); // (1) C++20
template < class OutputIterator ,
class Size ,
class T = typename iterator_traits < OutputIterator >:: value_type >
constexpr OutputIterator
fill_n ( OutputIterator first ,
Size n ,
const T & value ); // (1) C++26
template < class ExecutionPolicy ,
class ForwardIterator ,
class Size ,
class T >
ForwardIterator
fill_n ( ExecutionPolicy && exec ,
ForwardIterator first ,
Size n ,
const T & value ); // (2) C++17
template < class ExecutionPolicy ,
class ForwardIterator ,
class Size ,
class T = typename iterator_traits < ForwardIterator >:: value_type >
ForwardIterator
fill_n ( ExecutionPolicy && exec ,
ForwardIterator first ,
Size n ,
const T & value ); // (2) C++26
}
概要
イテレータ範囲[first, first + n)のすべての要素に指定された値を書き込む。
適格要件
value は output iterator へ書き込み可能でなければならない。
Size は integral type に変換可能でなければならない。
効果
n が 1 以上の場合は [first,first + n) 内の全ての要素に value を代入し、そうでない場合は何もしない。
戻り値
C++98 まで
無し
C++11 から
n が 1 以上の場合は first + n、そうでない場合は first を返す。
計算量
n が 1 以上の場合は n 回、そうでない場合は 0 回の代入を行う。
備考
(1), (2) :
C++26 : valueパラメータとして波カッコ初期化{}を受け付ける
例
基本的な使い方
出力
3,3,3,3,3,3,3,3,3,3,
波カッコ初期化を入力として使用する (C++26)
出力
1,2
1,2
1,2
0,0
0,0
実装例
template < class OutputIterator , class Size , class T >
#if __cplusplus >= 201103L
OutputIterator
#else
void
#endif
fill_n ( OutputIterator first , Size n , const T & value ) {
while ( n -- > 0 )
* first ++ = value ;
#if __cplusplus >= 201103L
return first ;
#endif
}
処理系
Clang :
GCC : 9.1.0 ✅
2つめの例(波カッコ初期化子リストによる呼び出し、C++26)は15.1.0から。
Visual C++ : 2010 ✅ , 2012 ✅ , 2013 ✅ , 2015 ✅
C++11への対応(戻り値の変更)は2012から。
関連項目
参照