다음을 통해 공유


Visual C++에서 list::list STL 함수 사용

이 문서에서는 Visual C++에서 STL 함수를 list::list 사용하는 방법을 보여 줍니다.

원래 제품 버전: Visual C++
원래 KB 번호: 158091

필수 헤더

<list>

프로토타입

explicit list(const A& al = A());
explicit list(size_type n, const T& v = T(), const A& al = A());
list(const list& x);
list(const_iterator first, const_iterator last, const A& al = A());

참고 항목

프로토타입의 클래스/매개 변수 이름이 헤더 파일의 버전과 일치하지 않을 수 있습니다. 일부는 가독성을 개선하기 위해 수정되었습니다.

설명

첫 번째 생성자는 빈 초기 제어 시퀀스를 지정합니다. 두 번째 생성자는 값 x요소의 n 반복을 지정합니다. 세 번째 생성자는 에 의해 x제어되는 시퀀스의 복사본을 지정합니다. 마지막 생성자는 시퀀스(first, last)를 지정합니다. 모든 생성자는 할당자 개체 al또는 복사 생성자에 대해 할당자에 x.get_allocator()저장하고 제어되는 시퀀스를 초기화합니다.

샘플 코드

//////////////////////////////////////////////////////////////////////
// Compile options needed: -GX
// list.cpp : demonstrates the different constructors for list<T>
// Functions:
//    list::list
// Copyright (c) 1996 Microsoft Corporation. All rights reserved.
//////////////////////////////////////////////////////////////////////

#include <list>
#include <string>
#include <iostream>

#if _MSC_VER > 1020   // if VC++ version is > 4.2
   using namespace std;  // std c++ libs implemented in std
   #endif

typedef list<string, allocator<string> > LISTSTR;

// Try each of the four constructors
void main()
{
    LISTSTR::iterator i;
    LISTSTR test;                   // default constructor
    test.insert(test.end(), "one");
    test.insert(test.end(), "two");
    LISTSTR test2(test);            // construct from another list
    LISTSTR test3(3, "three");      // add several <T>'s
    LISTSTR test4(++test3.begin(),  // add part of another list
             test3.end());
    // Print them all out
    // one two
    for (i =  test.begin(); i != test.end(); ++i)
        cout << *i << " ";
    cout << endl;
    // one two
    for (i =  test2.begin(); i != test2.end(); ++i)
        cout << *i << " ";
    cout << endl;
    // three three three
    for (i =  test3.begin(); i != test3.end(); ++i)
        cout << *i << " ";
    cout << endl;
    // three three
    for (i =  test4.begin(); i != test4.end(); ++i)
        cout << *i << " ";
    cout << endl;
}

프로그램 출력은 다음과 같습니다.

one two
one two
three three three
three three