Share via


방법: .NET 컬렉션에서 STL/CLR 컨테이너로 변환

이 항목에서는 .NET 컬렉션을 해당 STL/CLR 컨테이너로 변환하는 방법을 보여 줍니다. 예를 들어 .NET List<T> 을 STL/CLR 벡터 로 변환하는 방법과 .NET Dictionary<TKey,TValue> 을 STL/CLR 으로 변환하는 방법을 보여 주지만 프로시저는 모든 컬렉션 및 컨테이너에 대해 유사합니다.

컬렉션에서 컨테이너를 만들려면

  1. 전체 컬렉션을 변환하려면 STL/CLR 컨테이너를 만들고 컬렉션을 생성자에 전달합니다.

    첫 번째 예제에서는 이 절차를 보여 줍니다.

또는

  1. collection_adapter 개체를 만들어 제네릭 STL/CLR 컨테이너를 만듭니다. 이 템플릿 클래스는 .NET 컬렉션 인터페이스를 인수로 사용합니다. 지원되는 인터페이스를 확인하려면 collection_adapter(STL/CLR)를 참조하세요.

  2. .NET 컬렉션의 내용을 컨테이너에 복사합니다. STL/CLR 알고리즘을 사용하거나 .NET 컬렉션을 반복하고 각 요소의 복사본을 STL/CLR 컨테이너에 삽입하여 수행할 수 있습니다.

    두 번째 예제에서는 이 절차를 보여 줍니다.

예제

이 예제에서는 제네릭 List<T> 을 만들고 5개의 요소를 추가합니다. 그런 다음 인수로 vector 사용하는 using 생성자를 만듭니다 IEnumerable<T> .

// cliext_convert_list_to_vector.cpp
// compile with: /clr

#include <cliext/adapter>
#include <cliext/algorithm>
#include <cliext/vector>

using namespace System;
using namespace System::Collections;
using namespace System::Collections::Generic;

int main(array<System::String ^> ^args)
{
    List<int> ^primeNumbersColl = gcnew List<int>();
    primeNumbersColl->Add(2);
    primeNumbersColl->Add(3);
    primeNumbersColl->Add(5);
    primeNumbersColl->Add(7);
    primeNumbersColl->Add(11);

    cliext::vector<int> ^primeNumbersCont =
        gcnew cliext::vector<int>(primeNumbersColl);

    Console::WriteLine("The contents of the cliext::vector are:");
    cliext::vector<int>::const_iterator it;
    for (it = primeNumbersCont->begin(); it != primeNumbersCont->end(); it++)
    {
        Console::WriteLine(*it);
    }
}
The contents of the cliext::vector are:
2
3
5
7
11

이 예제에서는 제네릭 Dictionary<TKey,TValue> 을 만들고 5개의 요소를 추가합니다. 그런 다음 간단한 STL/CLR 컨테이너로 래핑 Dictionary<TKey,TValue> 하는 메서드를 만듭니 collection_adapter 다. 마지막으로 , 를 반복collection_adapter하여 내용을 만들고 map 복사 Dictionary<TKey,TValue>map 합니다. 이 프로세스 중에 함수를 사용하여 새 쌍을 make_pair 만들고 새 쌍을 에 직접 map삽입합니다.

// cliext_convert_dictionary_to_map.cpp
// compile with: /clr

#include <cliext/adapter>
#include <cliext/algorithm>
#include <cliext/map>

using namespace System;
using namespace System::Collections;
using namespace System::Collections::Generic;

int main(array<System::String ^> ^args)
{
    System::Collections::Generic::Dictionary<float, int> ^dict =
        gcnew System::Collections::Generic::Dictionary<float, int>();
    dict->Add(42.0, 42);
    dict->Add(13.0, 13);
    dict->Add(74.0, 74);
    dict->Add(22.0, 22);
    dict->Add(0.0, 0);

    cliext::collection_adapter<System::Collections::Generic::IDictionary<float, int>> dictAdapter(dict);
    cliext::map<float, int> aMap;
    for each (KeyValuePair<float, int> ^kvp in dictAdapter)
    {
        cliext::pair<float, int> aPair = cliext::make_pair(kvp->Key, kvp->Value);
        aMap.insert(aPair);
    }

    Console::WriteLine("The contents of the cliext::map are:");
    cliext::map<float, int>::const_iterator it;
    for (it = aMap.begin(); it != aMap.end(); it++)
    {
        Console::WriteLine("Key: {0:F} Value: {1}", it->first, it->second);
    }
}
The contents of the cliext::map are:
Key: 0.00 Value: 0
Key: 13.00 Value: 13
Key: 22.00 Value: 22
Key: 42.00 Value: 42
Key: 74.00 Value: 74

참고 항목

STL/CLR 라이브러리 참조
adapter(STL/CLR)
방법: STL/CLR 컨테이너에서 .NET 컬렉션으로 변환