memory_uninitialized_fill_n
memory_uninitialized_fill_n (std::uninitialized_fill_n)
<memory> 헤더에 정의되어 있어요. first에서 시작하는 대상 범위의 처음 count개 요소를 주어진 값 value로 구성해요. std::uninitialized_copy 계열의 "채우기" 버전이에요.
출처: cppreference
본문
template< class NoThrowForwardIt, class Size, class T >
NoThrowForwardIt uninitialized_fill_n( NoThrowForwardIt first, Size count, const T& value ); // (1) constexpr since C++26
template< class ExecutionPolicy, class NoThrowForwardIt, class Size, class T >
NoThrowForwardIt uninitialized_fill_n( ExecutionPolicy&& policy, NoThrowForwardIt first, Size count, const T& value ); // (2)
- (1) 대상 범위의 처음
count개 요소를 다음과 같이 값value로 구성해요.
for (; count > 0; ++first, --count)
::new (voidify(*first))
typename std::iterator_traits<NoThrowForwardIt>::value_type(value);
return first;
초기화 중 예외가 발생하면 이미 구성된 객체들이 미지정 순서로 파괴돼요.
- (2) (1)과 같지만
policy에 따라 실행돼요.
매개변수
first: 초기화할 요소 범위의 시작count: 구성할 요소 수value: 요소를 구성할 값policy: 사용할 실행 정책
반환값
마지막으로 구성한 요소 다음의 반복자.
예제
#include <iostream>
#include <memory>
#include <string>
int main()
{
constexpr int n{4};
alignas(alignof(std::string)) char out[n * sizeof(std::string)];
auto first{reinterpret_cast<std::string*>(out)};
auto last = std::uninitialized_fill_n(first, n, "example");
for (auto i{first}; i != last; ++i)
std::cout << *i << ' ';
std::cout << '\n';
std::destroy(first, last);
}