list_list
list_list (std::list::list — 생성자)
std::list 객체를 만드는 생성자들이에요. 기본 생성자부터 개수+값, 이터레이터 범위, 할당자, 복사·이동까지 다양한 방식으로 초기화할 수 있어요.
출처: cppreference
본문
주요 시그니처는 다음과 같아요.
list();
explicit list( const Allocator& alloc ); // (2)
explicit list( size_type count, const Allocator& alloc = Allocator() ); // (3)
list( size_type count, const T& value, const Allocator& alloc = Allocator() ); // (4)
template< class InputIt >
list( InputIt first, InputIt last, const Allocator& alloc = Allocator() ); // (5)
template< container-compatible-range<T> R >
list( std::from_range_t, R&& rg, const Allocator& alloc... ); // (from_range)
여러 오버로드가 있는데, 핵심은 다음과 같아요.
- (1)(2) 기본 생성자 / 할당자만 받는 생성자: 빈 컨테이너를 만들어요.
- (3)
count개만큼의 기본 삽입된(default-inserted) 원소를 가진 컨테이너를 만들어요. - (4)
count개의value복사본을 가진 컨테이너를 만들어요. - (5) 범위
[first, last)의 원소들을 가진 컨테이너를 만들어요. - from_range 생성자: 범위
rg의 원소들을 가진 컨테이너를 만들어요.
list의 복잡도는 여러 생성자에 걸쳐 보통 선형(linear)이에요.
예제
#include <list>
#include <iostream>
int main()
{
std::list<int> l{1, 2, 3}; // 초기화 목록
std::list<int> l2(l); // 복사
l2.push_back(4); // {1, 2, 3, 4}
for (int x : l2) std::cout << x << ' ';
}