7. 다시 singly_ll 클래스 구현으로 돌아오겠습니다. 앞에서 singly_ll 클래스를 위한 반복자를 정의했으므로, begin()end() 함수를 추가합니다. 두 함수의 const 버전도 함께 추가합니다.

    singly_ll_iterator begin() { return singly_ll_iterator(head); }
    singly_ll_iterator end() { return singly_ll_iterator(NULL); }
    singly_ll_iterator begin() const { return singly_ll_iterator(head); }
    singly_ll_iterator end() const { return singly_ll_iterator(NULL); }
    

      8. 기본 생성자, 깊은 복사를 위한 복사 생성자, 초기화 리스트(initialize list)를 사용하는 생성자를 추가합니다.

        singly_ll() = default;
        singly_ll(const singly_ll& other) : head(NULL)
        {
            if (other.head)
            {
                head = new node{0, NULL};
                auto cur = head;
                auto it = other.begin();
                while (true)
                {
                    cur->data = *it;
    
                    auto tmp = it;
                    ++tmp;
                    if (tmp == other.end())
                        break;
    
                    cur->next = new node{0, NULL};
                    cur = cur->next;
                    it = tmp;
                }
            }
        }
    
        singly_ll(const std::initializer_list<int>& ilist) : head(NULL)
        {
            for (auto it = std::rbegin(ilist); it != std::rend(ilist); it++)
                push_front(*it);
        }
    };
    
    신간 소식 구독하기
    뉴스레터에 가입하시고 이메일로 신간 소식을 받아 보세요.