다음을 통해 공유


fill_n

특정 요소로 시작하는 범위에서 지정된 요소 번호에 새 값을 할당합니다.

template<class OutputIterator, class Size, class Type> 
   OutputIterator fill_n( 
      OutputIterator First,  
      Size Count,  
      const Type& Val 
   ); 

매개 변수

  • First
    Val 값을 할당할 범위에 있는 첫 번째 요소의 위치를 가리키는 출력 반복기입니다.

  • Count
    값을 할당하게 되는 요소의 수를 지정하는 서명되었거나 서명되지 않은 정수 형식입니다.

  • Val
    범위 [First, 처음 + 개수)에서 요소에 할당되는 값입니다.

반환 값

Count > 0인 경우 반복기가 채워진 마지막 요소 다음 요소로 진행하고, 그렇지 않으면 첫 번째 요소로 진행합니다.

설명

대상 범위는 유효해야 합니다. 모든 포인터는 역참조할 수 있어야 하며 마지막 위치는 처음부터 증분 만큼 접근 가능합니다. 복잡성은 범위 크기의 선형입니다.

fill_n은 두 가지 관련된 폼이 있습니다.

이러한 기능이 동작하는 방식에 대한 자세한 내용은 Checked Iterators을 참조하십시오.

예제

// alg_fill_n.cpp
// compile using /EHsc
#include <vector>
#include <algorithm>
#include <iostream>

int main() 
{
    using namespace std;
    vector <int> v;

    for ( auto i = 0 ; i < 9 ; ++i )
        v.push_back( 0 );

    cout << "  vector v = ( " ;
    for ( const auto &w : v )
        cout << w << " ";
    cout << ")" << endl;

    // Fill the first 3 positions with a value of 1, saving position.
    auto pos = fill_n( v.begin(), 3, 1 );

    cout << "modified v = ( " ;
    for ( const auto &w : v )
        cout << w << " ";
    cout << ")" << endl;

    // Fill the next 3 positions with a value of 2, using last position.
    fill_n( pos, 3, 2 );

    cout << "modified v = ( " ;
    for ( const auto &w : v )
        cout << w << " ";
    cout << ")" << endl;

    // Fill the last 3 positions with a value of 3, using relative math.
    fill_n( v.end()-3, 3, 3 );

    cout << "modified v = ( " ;
    for ( const auto &w : v )
        cout << w << " ";
    cout << ")" << endl;
}

Output

  vector v = ( 0 0 0 0 0 0 0 0 0 )
modified v = ( 1 1 1 0 0 0 0 0 0 )
modified v = ( 1 1 1 2 2 2 0 0 0 )
modified v = ( 1 1 1 2 2 2 3 3 3 )

요구 사항

헤더: <algorithm>

네임스페이스: std

참고 항목

참조

표준 템플릿 라이브러리