algorithm_fill_n

algorithm_fill_n (지정 개수 채우기)

std::fill_n은 대상 범위의 처음 count개 요소에 지정한 값 value를 할당해요. std::fill과 달리 개수를 명시적으로 지정해요.

출처: cppreference

본문

std::fill_n<algorithm> 헤더에 정의되어 있어요.

template< class OutputIt, class Size, class T >
OutputIt fill_n( OutputIt first, Size count, const T& value );

count가 양수이면 대상 범위 [first, std::next(first, count))의 모든 요소에 value를 할당해요. 그렇지 않으면 아무것도 하지 않아요. 병렬 실행 정책을 받는 오버로드도 있어요.

반환값 (Return value)

대상 범위의 끝(past-the-end) 반복자예요. count가 양수가 아니면 first를 돌려줘요.

복잡도 (Complexity)

정확히 max(count, 0)번의 배정이 필요해요.

예제 (Example)

#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>

int main()
{
    std::vector<int> v1{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    std::fill_n(v1.begin(), 5, -1);
    std::copy(begin(v1), end(v1), std::ostream_iterator<int>(std::cout, " "));
    std::cout << '\n';

    std::vector<int> v2{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    std::fill_n(v2.begin(), v2.size(), -1);
    std::copy(begin(v2), end(v2), std::ostream_iterator<int>(std::cout, " "));
    std::cout << '\n';
}

출력:

-1 -1 -1 -1 -1 5 6 7 8 9
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1

더 알아보기 (Learn more)

cppreference