list_assign

list_assign (std::list::assign — 내용 교체)

std::list의 내용 전체를 새 값들로 통째로 교체하는 멤버 함수예요. 개수+값, 이터레이터 범위, 초기화 목록 방식으로 지정할 수 있어요.

출처: cppreference

본문

시그니처는 다음과 같아요.

void assign( size_type count, const T& value );        // (1)
template< class InputIt >
void assign( InputIt first, InputIt last );            // (2)
void assign( std::initializer_list<T> ilist );         // (3) (since C++11)

컨테이너의 내용을 교체해요.

(1) 내용을 값 value의 복사본 count개로 교체해요.

(2) 내용을 범위 [first, last)의 원소 복사본들로 교체해요.

두 인자 중 하나가 *this 안으로의 이터레이터라면 동작이 정의되지 않아요.

InputIt이 정수 타입이라면 (2) 오버로드는 (1)과 같은 효과를 가져요. (until C++11)

복잡도

count 또는 std::distance(first, last)에 선형(linear)이에요.

예제

#include <list>
#include <iostream>
int main()
{
    std::list<int> l;
    l.assign({1, 2, 3});
    for (int x : l) std::cout << x << ' ';  // 1 2 3
}

더 알아보기 (Learn more)

cppreference