Namespaces
Variants

std::ranges:: fill

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>
함수 시그니처
(1)
template < class T, std:: output_iterator < const T & > O, std:: sentinel_for < O > S >
constexpr O fill ( O first, S last, const T & value ) ;
(C++20부터)
(C++26까지)
template < class O, std:: sentinel_for < O > S, class T = std:: iter_value_t < O > >

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

constexpr O fill ( O first, S last, const T & value ) ;
(C++26부터)
(2)
template < class T, ranges:: output_range < const T & > R >
constexpr ranges:: borrowed_iterator_t < R > fill ( R && r, const T & value ) ;
(C++20부터)
(C++26까지)
template < class R, class T = ranges:: range_value_t < R > >

requires ranges:: output_range < R, const T & >

constexpr ranges:: borrowed_iterator_t < R > fill ( R && r, const T & value ) ;
(C++26부터)
1) 주어진 value 를 범위 [ first , last ) 내의 요소들에 할당합니다.
2) (1) 와 동일하지만, r 를 소스 범위로 사용하며, 마치 ranges:: begin ( r ) first 로, ranges:: end ( r ) last 로 사용하는 것과 같습니다.

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

목차

매개변수

first, last - 수정할 요소들의 범위 를 정의하는 반복자-감시자 쌍
r - 수정할 요소들의 범위
value - 할당할 값

반환값

last 와 비교 시 동일함을 나타내는 출력 반복자입니다.

복잡도

정확히 last - first 번의 할당.

가능한 구현

struct fill_fn
{
    template<class O, std::sentinel_for<O> S, class T = std::iter_value_t<O>>
    requires std::output_iterator<O, const T&>
    constexpr O operator()(O first, S last, const T& value) const
    {
        while (first != last)
            *first++ = value;
        return first;
    }
    template<class R, class T = ranges::range_value_t<R>>
    requires ranges::output_range<R, const T&>
    constexpr ranges::borrowed_iterator_t<R> operator()(R&& r, const T& value) const
    {
        return (*this)(ranges::begin(r), ranges::end(r), value);
    }
};
inline constexpr fill_fn fill;

참고 사항

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

예제

#include <algorithm>
#include <complex>
#include <iostream>
#include <vector>
void println(const auto& seq)
{
    for (const auto& e : seq)
        std::cout << e << ' ';
    std::cout << '\n';
}
int main()
{
    std::vector<int> v{0, 1, 2, 3, 4, 5};
    // 오버로드 (1)을 사용하여 모든 요소를 -1로 설정
    std::ranges::fill(v.begin(), v.end(), -1);
    println(v);
    // 오버로드 (2)를 사용하여 모든 요소를 10으로 설정
    std::ranges::fill(v, 10);
    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(nums, {4, 2}); // T가 추론됨
    #else
        std::ranges::fill(nums, std::complex<double>{4, 2});
    #endif
    println(nums);
}

출력:

-1 -1 -1 -1 -1 -1
10 10 10 10 10 10
(1,3) (2,2) (4,8)
(4,2) (4,2) (4,2)

참고 항목

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