더북(TheBook)

다른 컨테이너의 타입 T 원소들로 vector<T> 컨테이너를 생성하고 초기화할 수 있다. 초깃값으로 사용할 원소들의 범위는 반복자 쌍으로 지정한다. 예제로 살펴보자.

std::array<std::string, 5> words {"one", "two", "three", "four", "five"};
std::vector<std::string> words_copy {std::begin(words), std::end(words)};

words_copy 벡터는 words 배열 컨테이너의 원소들로 초기화된다. words_copy의 초기화 범위를 이동 반복자로 지정했다면 원소들은 words 배열에서 words_copy 벡터로 이동했을 것이다. 다음 코드는 이동 반복자로 원소들을 이동시킨다.

std::vector<std::string> words_copy {std::make_move_iterator(std::begin(words)),
                                     std::make_move_iterator(std::end(words))};

words_copy 벡터는 앞 코드처럼 초기화되었다고 하자. 원소들은 복제가 아니라 이동한 것이므로 words 배열은 이제 빈 문자열 ""를 나타내는 string 객체만 갖는다.

신간 소식 구독하기
뉴스레터에 가입하시고 이메일로 신간 소식을 받아 보세요.