memory_uninitialized_fill

memory_uninitialized_fill (std::uninitialized_fill)

<memory> 헤더에 정의되어 있어요. 대상 범위 [first, last)의 요소를 주어진 값 value로 구성해요.

출처: cppreference

본문

template< class NoThrowForwardIt, class T >
void uninitialized_fill( NoThrowForwardIt first, NoThrowForwardIt last, const T& value );  // (1) constexpr since C++26

template< class ExecutionPolicy, class NoThrowForwardIt, class T >
void uninitialized_fill( ExecutionPolicy&& policy, NoThrowForwardIt first, NoThrowForwardIt last, const T& value );  // (2)
  • (1) 대상 범위 [first, last)의 요소를 값 value로 구성해요. 초기화 중 예외가 발생하면 이미 구성된 객체들이 미지정 순서로 파괴돼요.
  • (2) (1)과 같지만 policy에 따라 실행돼요.

매개변수

  • first, last: 초기화할 요소 범위를 정의하는 반복자 쌍
  • 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{first + n};
    std::uninitialized_fill(first, last, "hello");
    for (auto it{first}; it != last; ++it)
        std::cout << *it << ' ';
    std::cout << '\n';
    std::destroy(first, last);
}

출력:

hello hello hello hello

더 알아보기 (Learn more)

cppreference