ranges_uninitialized_fill_n
ranges_uninitialized_fill_n (ranges::uninitialized_fill_n)
<memory> 헤더에 정의되어 있어요. first부터 시작해 count개의 요소를 주어진 값 value로 초기화해 구성해요. std::uninitialized_fill_n의 ranges 버전(니블로이드, niebloid)이에요. C++20부터 도입됐어요.
출처: cppreference
본문
호출 시그니처:
template< /*nothrow-forward-iterator*/ I, class T >
requires std::constructible_from<std::iter_value_t<I>, const T&>
I uninitialized_fill_n( I first, std::iter_difference_t<I> count,
const T& value ); // (1) since C++20, constexpr since C++26
template< /*execution-policy*/ Ep, /*nothrow-random-access-iterator*/ I,
class T = std::iter_value_t<I> >
requires std::constructible_from<std::iter_value_t<I>, const T&>
I uninitialized_fill_n( Ep&& exec, I first, std::iter_difference_t<I> count,
const T& value ); // (2) since C++26
- (1) 대상 범위
first + [0, count)의 요소들을 주어진 값value로 구성해요. 초기화 중 예외가 발생하면 이미 구성된 객체들이 미지정 순서로 파괴돼요. - (2) (1)과 같지만
policy에 따라 실행돼요. (C++26)
이 페이지에 설명된 함수 같은 개체들은 알고리즘 함수 객체(니블로이드)라서, 명시적 템플릿 인자 목록을 쓸 수 없고, 인자 연관 탐색으로도 보이지 않아요.
매개변수
first: 초기화할 요소 범위의 시작count: 구성할 요소 수value: 요소를 구성할 값policy: 사용할 실행 정책
반환값
마지막으로 구성한 요소 다음의 반복자.
예외
대상 범위의 요소 구성 중 던져진 어떤 예외든 던질 수 있어요. (2)에서는 병렬화에 필요한 임시 메모리가 없으면 std::bad_alloc이 던져져요.
예제
#include <iostream>
#include <memory>
#include <string>
int main()
{
constexpr int n{3};
alignas(alignof(std::string)) char out[n * sizeof(std::string)];
try
{
auto first{reinterpret_cast<std::string*>(out)};
auto last = std::ranges::uninitialized_fill_n(first, n, "cppreference");
for (auto it{first}; it != last; ++it)
std::cout << *it << '\n';
std::ranges::destroy(first, last);
}
catch (...) { std::cout << "Exception!\n"; }
}
출력:
cppreference
cppreference
cppreference