forward_list_forward_list

forward_list_forward_list (std::forward_list::forward_list — 생성자)

std::forward_list 객체를 만드는 생성자들이에요. 기본 생성자부터 개수+값, 이터레이터 범위, 할당자, 복사·이동까지 다양한 방식으로 초기화할 수 있어요.

출처: cppreference

본문

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

forward_list() : forward_list(Allocator()) {}   // (1)
explicit forward_list( const Allocator& alloc );                 // (2)
explicit forward_list( size_type count, const Allocator& alloc = Allocator() ); // (3)
forward_list( size_type count, const T& value, const Allocator& alloc = Allocator() ); // (4)
template< class InputIt >
forward_list( InputIt first, InputIt last, const Allocator& alloc = Allocator() ); // (5)
template< container-compatible-range<T> R >
forward_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의 원소들을 가진 컨테이너를 만들어요.

forward_list의 복잡도는 여러 생성자에 걸쳐 보통 선형(linear)이에요.

예제

#include <forward_list>
#include <iostream>
int main()
{
    std::forward_list<int> fl{1, 2, 3};       // 초기화 목록
    std::forward_list<int> fl2(fl);           // 복사
    fl2.push_front(0);                        // {0, 1, 2, 3}
    for (int x : fl2) std::cout << x << ' ';
}

더 알아보기 (Learn more)

cppreference