Namespaces
Variants

std::ranges:: 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
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
Constrained algorithms
All names in this menu belong to namespace std::ranges
Non-modifying sequence operations
Modifying sequence operations
Partitioning operations
Sorting operations
Binary search operations (on sorted ranges)
Set operations (on sorted ranges)
Heap operations
Minimum/maximum operations
Permutation operations
Fold operations
Operations on uninitialized storage
Return types
헤더 파일에 정의됨 <algorithm>
함수 시그니처
template < class T, std:: output_iterator < const T & > O >
constexpr O fill_n ( O first, std:: iter_difference_t < O > n, const T & value ) ;
(C++20부터)
(C++26 이전)
template < class O, class T = std:: iter_value_t < O > >

requires std:: output_iterator < O, const T & >

constexpr O fill_n ( O first, std:: iter_difference_t < O > n, const T & value ) ;
(C++26부터)

주어진 value 를 범위 [ first , first + n ) 내의 모든 요소에 할당합니다.

이 페이지에서 설명하는 함수형 개체들은 algorithm function objects (일반적으로 niebloids 로 알려진)입니다. 즉:

목차

매개변수

first - 수정할 요소 범위의 시작
n - 수정할 요소의 개수
value - 할당할 값

반환값

first + n 와 동일하게 비교되는 출력 반복자.

복잡도

정확히 n 번의 할당.

가능한 구현

struct fill_n_fn
{
    template<class O, class T = std::iter_value_t<O>>
    requires std::output_iterator<O, const T&>
    constexpr O operator()(O first, std::iter_difference_t<O> n, const T& value) const
    {
        for (std::iter_difference_t<O> i {}; i != n; ++first, ++i)
            *first = value;
        return first;
    }
};
inline constexpr fill_n_fn fill_n {};

참고 사항

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

예제

#include <algorithm>
#include <complex>
#include <iostream>
#include <string>
#include <vector>
void println(const auto& v)
{
    for (const auto& elem : v)
        std::cout << ' ' << elem;
    std::cout << '\n';
}
int main()
{
    constexpr auto n{8};
    std::vector<std::string> v(n, "▓▓░░");
    println(v);
    std::ranges::fill_n(v.begin(), n, "░░▓▓");
    println(v);
    std::vector<std::complex<double>> nums{{1, 3}, {2, 2}, {4, 8}};
    println(nums);
    #ifdef __cpp_lib_algorithm_default_value_type
        std::ranges::fill_n(nums.begin(), 2, {4, 2});
    #else
        std::ranges::fill_n(nums.begin(), 2, std::complex<double>{4, 2});
    #endif
    println(nums);
}

출력:

 ▓▓░░ ▓▓░░ ▓▓░░ ▓▓░░ ▓▓░░ ▓▓░░ ▓▓░░ ▓▓░░
 ░░▓▓ ░░▓▓ ░░▓▓ ░░▓▓ ░░▓▓ ░░▓▓ ░░▓▓ ░░▓▓
 (1,3) (2,2) (4,8)
 (4,2) (4,2) (4,8)

참고 항목

범위의 요소들에 특정 값을 할당함
(알고리즘 함수 객체)
지정된 개수의 요소들을 새로운 위치로 복사함
(알고리즘 함수 객체)
함수의 결과를 범위에 저장함
(알고리즘 함수 객체)
요소들의 범위에 함수를 적용함
(알고리즘 함수 객체)
균일 난수 비트 생성기로부터의 난수로 범위를 채움
(알고리즘 함수 객체)
주어진 값을 범위 내 N개 요소에 복사 할당함
(함수 템플릿)