STL/CLR 컨테이너

STL/CLR 라이브러리, 표준 C++ 라이브러리에서 발견 되는 같은 컨테이너 있지만의 관리 되는 환경에서 실행 되는.NET 프레임 워크입니다.표준 템플릿 라이브러리 (STL) 이미 익숙한 경우 STL/CLR 대상 공용 언어 런타임 (CLR) 코드를 업그레이드 하는 동안 이미 개발한 기술을 사용 하 여 계속 하는 최선의 방법입니다.

이 문서에서는 STL/CLR 컨테이너와 같은 컨테이너 요소를 컨테이너에 삽입할 수 있습니다 및 소유권의 컨테이너에서 요소로 발급 요소 유형의 요구 사항 간략하게를 설명 합니다.해당 되는 경우 네이티브 표준 템플릿 라이브러리 및 STL/CLR 차이 나와 있습니다.

컨테이너 요소에 대 한 요구 사항

STL 컨테이너에 삽입 되는 모든 요소가 특정 지침을 준수 해야 합니다.자세한 내용은 STL/CLR 컨테이너 요소에 대 한 요구 사항를 참조하십시오.

유효한 컨테이너 요소입니다.

STL/CLR 컨테이너 두 형식의 요소 중 하나를 저장할 수 있습니다.

  • 참조 형식으로 처리 합니다.

  • 참조 형식

  • Unboxed 값 형식입니다.

Boxed 값 형식은 모든 STL/CLR 컨테이너에 삽입할 수 없습니다.

Bb385236.collapse_all(ko-kr,VS.110).gif참조 형식에 대 한 핸들

STL/CLR 컨테이너에는 참조 형식에 대 한 핸들을 삽입할 수 있습니다.CLR을 대상으로 하는 C++에 대 한 핸들에 대 한 포인터에는 네이티브 C++와 유사 합니다.자세한 내용은 개체 연산자에 대한 핸들(^)(C++ 구성 요소 확장)를 참조하십시오.

Bb385236.collapse_all(ko-kr,VS.110).gif예제

다음 예제에서는 Employee 개체에 대 한 핸들을 삽입 하는 방법에 cliext::set.

// cliext_container_valid_reference_handle.cpp
// compile with: /clr

#include <cliext/set>

using namespace cliext;
using namespace System;

ref class Employee
{
public:
    // STL containers might require a public constructor, so it
    // is a good idea to define one.
    Employee() :
        name(nullptr),
        employeeNumber(0) { }

    // All STL containers require a public copy constructor.
    Employee(const Employee% orig) :
        name(orig.name),
        employeeNumber(orig.employeeNumber) { }

    // All STL containers require a public assignment operator.
    Employee% operator=(const Employee% orig)
    {
        if (this != %orig)
        {
            name = orig.name;
            employeeNumber = orig.employeeNumber;
        }

        return *this;
    }

    // All STL containers require a public destructor.
    ~Employee() { }

    // Associative containers such as maps and sets
    // require a comparison operator to be defined
    // to determine proper ordering.
    bool operator<(const Employee^ rhs)
    {
        return (employeeNumber < rhs->employeeNumber);
    }

    // The employee's name.
    property String^ Name
    {
        String^ get() { return name; }
        void set(String^ value) { name = value; }
    }

    // The employee's employee number.
    property int EmployeeNumber
    {
        int get() { return employeeNumber; }
        void set(int value) { employeeNumber = value; }
    }

private:
    String^ name;
    int employeeNumber;
};

int main()
{
    // Create a new employee object.
    Employee^ empl1419 = gcnew Employee();
    empl1419->Name = L"Darin Lockert";
    empl1419->EmployeeNumber = 1419;

    // Add the employee to the set of all employees.
    set<Employee^>^ emplSet = gcnew set<Employee^>();
    emplSet->insert(empl1419);

    // List all employees of the company.
    for each (Employee^ empl in emplSet)
    {
        Console::WriteLine("Employee Number {0}: {1}",
            empl->EmployeeNumber, empl->Name);
    }

    return 0;
}

Bb385236.collapse_all(ko-kr,VS.110).gif참조 형식

참조 형식에 대 한 핸들이 아니라 참조 형식이 STL/CLR 컨테이너에 삽입이 가능 합니다.주된 차이점은 여기 참조 형식의 컨테이너를 삭제 하면 소멸자가 해당 컨테이너 내의 모든 요소에 대 한 것 이라고 합니다.핸들 형식 참조 하는 컨테이너에 이러한 요소에 대 한 소멸자가 호출할 수 없습니다.

Bb385236.collapse_all(ko-kr,VS.110).gif예제

다음 예제에서는 Employee 개체로 삽입 하는 방법을 보여 줍니다 있는 cliext::set.

// cliext_container_valid_reference.cpp
// compile with: /clr

#include <cliext/set>

using namespace cliext;
using namespace System;

ref class Employee
{
public:
    // STL containers might require a public constructor, so it
    // is a good idea to define one.
    Employee() :
        name(nullptr),
        employeeNumber(0) { }

    // All STL containers require a public copy constructor.
    Employee(const Employee% orig) :
        name(orig.name),
        employeeNumber(orig.employeeNumber) { }

    // All STL containers require a public assignment operator.
    Employee% operator=(const Employee% orig)
    {
        if (this != %orig)
        {
            name = orig.name;
            employeeNumber = orig.employeeNumber;
        }

        return *this;
    }

    // All STL containers require a public destructor.
    ~Employee() { }

    // Associative containers such as maps and sets
    // require a comparison operator to be defined
    // to determine proper ordering.
    bool operator<(const Employee^ rhs)
    {
        return (employeeNumber < rhs->employeeNumber);
    }

    // The employee's name.
    property String^ Name
    {
        String^ get() { return name; }
        void set(String^ value) { name = value; }
    }

    // The employee's employee number.
    property int EmployeeNumber
    {
        int get() { return employeeNumber; }
        void set(int value) { employeeNumber = value; }
    }

private:
    String^ name;
    int employeeNumber;
};

int main()
{
    // Create a new employee object.
    Employee empl1419;
    empl1419.Name = L"Darin Lockert";
    empl1419.EmployeeNumber = 1419;

    // Add the employee to the set of all employees.
    set<Employee>^ emplSet = gcnew set<Employee>();
    emplSet->insert(empl1419);

    // List all employees of the company.
    for each (Employee^ empl in emplSet)
    {
        Console::WriteLine("Employee Number {0}: {1}",
            empl->EmployeeNumber, empl->Name);
    }

    return 0;
}

Bb385236.collapse_all(ko-kr,VS.110).gifUnboxed 값 형식

Unboxed 값 형식에 사용 되는 STL/CLR 컨테이너에 삽입할 수도 있습니다.Unboxed 값 형식이 되지 않은 값 형식인 박스형 참조 유형입니다.

값 형식 요소를 표준 값 형식의 같은 수는 int, 또는 사용자 정의 값 형식, 같은 것은 value class.자세한 내용은 클래스 및 구조체(C++ 구성 요소 확장)를 참조하십시오.

Bb385236.collapse_all(ko-kr,VS.110).gif예제

다음 예제에서는 첫 번째 예제는 직원 값 형식 클래스를 만들어 수정 합니다.이 값 형식 다음에 삽입 되는 cliext::set 첫 번째 예제에서와 마찬가지로.

// cliext_container_valid_valuetype.cpp
// compile with: /clr

#include <cliext/set>

using namespace cliext;
using namespace System;

value class Employee
{
public:
    // Associative containers such as maps and sets
    // require a comparison operator to be defined
    // to determine proper ordering.
    bool operator<(const Employee^ rhs)
    {
        return (employeeNumber < rhs->employeeNumber);
    }

    // The employee's name.
    property String^ Name
    {
        String^ get() { return name; }
        void set(String^ value) { name = value; }
    }

    // The employee's employee number.
    property int EmployeeNumber
    {
        int get() { return employeeNumber; }
        void set(int value) { employeeNumber = value; }
    }

private:
    String^ name;
    int employeeNumber;
};

int main()
{
    // Create a new employee object.
    Employee empl1419;
    empl1419.Name = L"Darin Lockert";
    empl1419.EmployeeNumber = 1419;

    // Add the employee to the set of all employees.
    set<Employee>^ emplSet = gcnew set<Employee>();
    emplSet->insert(empl1419);

    // List all employees of the company.
    for each (Employee empl in emplSet)
    {
        Console::WriteLine("Employee Number {0}: {1}",
            empl.EmployeeNumber, empl.Name);
    }

    return 0;
}

값 형식에 대 한 핸들을 컨테이너에 삽입 하려고 하면 컴파일러 오류 C3225 생성 됩니다.

Bb385236.collapse_all(ko-kr,VS.110).gif성능 및 메모리에 미치는 영향

핸들 형식 또는 값 형식으로 컨테이너의 요소를 참조 하려면 다음을 사용할지 여부를 결정할 때는 여러 가지 요인을 고려해 야 합니다.값 형식을 사용 하는 경우 요소를 컨테이너에 삽입 될 때마다 복사본의 요소가 될 수 있음을 기억 하십시오.작은 개체 삽입 되는 개체 클 경우 성능이 저하 될 수 있습니다 있지만 이것이 문제가 되지 않습니다.값 형식을 사용 하는 경우 또한 그 각 컨테이너 요소의 고유한 복사본을 해야 하기 때문에 동시에 여러 컨테이너에 요소 하나를 저장할 수 없습니다.

핸들을 사용 하 여 대신 형식을 참조 하는 경우 해당 컨테이너에 삽입 될 때 요소의 복사본을 만들 필요가 없기 때문에 성능을 증가할 수 있습니다.또한 달리 값 형식의 경우 동일한 요소 여러 컨테이너에 존재할 수 있습니다.그러나 핸들을 사용 하는 경우 핸들이 유효 하며이 참조 하는 개체가 다른 곳에서 프로그램에서 삭제 되지 않았습니다 것 되도록 주의 해야 합니다.

컨테이너와 소유권 문제

STL/CLR 컨테이너 값 의미에서 작동 합니다.요소를 컨테이너에 삽입할 때마다 해당 요소의 복사본이 삽입 됩니다.참조와 유사한 의미를 가져오려면 개체 자체가 아닌 개체에 대 한 핸들을 삽입할 수 있습니다.

일반 전화를 하거나 메서드 핸들 개체의 컨테이너를 지울 때 핸들을 참조 하는 개체 메모리에서 해제 되지 않습니다.명시적으로 개체를 삭제 하거나, 이러한 개체는 관리 되는 힙에 상주 하기 때문에 가비지 수집기가 개체가 더 이상 사용 하지를 결정 한 후 해당 메모리 공간을 허용 합니다.

참고 항목

참조

표준 템플릿 라이브러리