Namespaces
Variants

std:: fill_n

From cppreference.net
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Constrained algorithms, e.g. ranges::copy , ranges::sort , ...
Execution policies (C++17)
Non-modifying sequence operations
Batch operations
(C++17)
Search operations
Modifying sequence operations
Copy operations
(C++11)
(C++11)
Swap operations
Transformation operations
Generation operations
fill_n
Removing operations
Order-changing operations
(until C++17) (C++11)
(C++20) (C++20)
Sampling operations
(C++17)

Sorting and related operations
Partitioning operations
Sorting operations
Binary search operations
(on partitioned ranges)
Set operations (on sorted ranges)
Merge operations (on sorted ranges)
Heap operations
Minimum/maximum operations
Lexicographical comparison operations
Permutation operations
C library
Numeric operations
Operations on uninitialized memory
헤더 파일에 정의됨 <algorithm>
(1)
template < class OutputIt, class Size, class T >
OutputIt fill_n ( OutputIt first, Size count, const T & value ) ;
(C++20부터 constexpr)
(C++26까지)
template < class OutputIt, class Size,

class T = typename std:: iterator_traits
< OutputIt > :: value_type >
constexpr OutputIt fill_n ( OutputIt first, Size count,

const T & value ) ;
(C++26부터)
(2)
template < class ExecutionPolicy,

class ForwardIt, class Size, class T >
ForwardIt fill_n ( ExecutionPolicy && policy,

ForwardIt first, Size count, const T & value ) ;
(C++17부터)
(C++26까지)
template < class ExecutionPolicy,

class ForwardIt, class Size,
class T = typename std:: iterator_traits
< OutputIt > :: value_type >
ForwardIt fill_n ( ExecutionPolicy && policy,

ForwardIt first, Size count, const T & value ) ;
(C++26부터)
1) 주어진 value first 에서 시작하는 범위의 첫 번째 count 개 요소에 할당합니다. 단, count > 0 인 경우에만 해당하며, 그렇지 않으면 아무 작업도 수행하지 않습니다.
2) (1) 과 동일하지만, policy 에 따라 실행됩니다.
이 오버로드는 다음 모든 조건이 만족될 때만 오버로드 해결에 참여합니다:

std:: is_execution_policy_v < std:: decay_t < ExecutionPolicy >> true 입니다.

(C++20 이전)

std:: is_execution_policy_v < std:: remove_cvref_t < ExecutionPolicy >> true 입니다.

(C++20 이후)

다음 조건 중 하나라도 충족되면 프로그램의 형식이 잘못되었습니다:

목차

매개변수

first - 수정할 요소 범위의 시작
count - 수정할 요소의 개수
value - 할당할 값
policy - 사용할 실행 정책
타입 요구사항
-
OutputIt LegacyOutputIterator 요구사항을 충족해야 함
-
ForwardIt LegacyForwardIterator 요구사항을 충족해야 함

반환값

count > 0 인 경우 할당된 마지막 요소의 다음 위치를 가리키는 반복자, first 인 경우 그렇지 않으면.

복잡도

정확히 std:: max ( 0 , count ) 할당.

예외

ExecutionPolicy 라는 템플릿 매개변수를 사용하는 오버로드는 다음과 같이 오류를 보고합니다:

  • 알고리즘의 일부로 호출된 함수 실행 중 예외가 발생하고 ExecutionPolicy 표준 정책 중 하나인 경우, std::terminate 가 호출됩니다. 다른 ExecutionPolicy 의 경우 동작은 구현에 따라 정의됩니다.
  • 알고리즘이 메모리 할당에 실패할 경우, std::bad_alloc 이 throw됩니다.

가능한 구현

fill_n
template<class OutputIt, class Size,
         class T = typename std::iterator_traits<OutputIt>::value_type>
OutputIt fill_n(OutputIt first, Size count, const T& value)
{
    for (Size i = 0; i < count; i++)
        *first++ = value;
    return first;
}

참고 사항

기능 테스트 매크로 표준 기능
__cpp_lib_algorithm_default_value_type 202403 (C++26) 목록 초기화 for algorithms ( 1,2 )

예제

#include <algorithm>
#include <complex>
#include <iostream>
#include <iterator>
#include <vector>
int main()
{
    std::vector<int> v1{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    // 처음 5개 요소의 값을 -1로 대체
    std::fill_n(v1.begin(), 5, -1);
    std::copy_n(v1.cbegin(), v1.size(), std::ostream_iterator<int>(std::cout, " "));
    std::cout << '\n';
    std::vector<std::complex<double>> nums{{1, 3}, {2, 2}, {4, 8}};
    #ifdef __cpp_lib_algorithm_default_value_type
        std::fill_n(nums.begin(), 2, {4, 2});
    #else
        std::fill_n(nums.begin(), 2, std::complex<double>{4, 2});
    #endif
    std::copy_n(nums.cbegin(), nums.size(),
                std::ostream_iterator<std::complex<double>>(std::cout, " "));
    std::cout << '\n';
}

출력:

-1 -1 -1 -1 -1 5 6 7 8 9
(4,2) (4,2) (4,8)

결함 보고서

다음의 동작 변경 결함 보고서들은 이전에 발표된 C++ 표준에 소급 적용되었습니다.

DR 적용 대상 게시된 동작 올바른 동작
LWG 283 C++98 T CopyAssignable 요구되었으나,
T 가 항상 OutputIt 에 기록 가능하지는 않음
대신 기록 가능해야 함으로 요구
LWG 426 C++98 복잡도 요구사항이 "정확히 count
회 할당"이었으나, 이는 count 가 음수일 경우 문제됨
count 가 양수가 아닐 경우
할당 수행 안 함
LWG 865 C++98 채우기 범위 바로 다음 요소의
위치가 반환되지 않았음
반환됨

참고 항목

주어진 범위의 모든 요소에 지정된 값을 복사-할당합니다
(function template)
지정된 개수의 요소에 값을 할당합니다
(algorithm function object)