방법: 변환 된.STL/CLR 컨테이너 NET 컬렉션
이 항목에서는 변환 하는 방법을 보여 줍니다.NET의 동등한 STL/CLR 컨테이너 컬렉션.예를 들어 우리가 변환 하는 방법에.NET List<T> STL/CLR을 벡터 및 변환 하는 방법에 있습니다.NET Dictionary<TKey, TValue> STL/CLR에 맵, 하지만 모든 컬렉션 및 컨테이너에 대 한 절차는 유사 합니다.
컬렉션에서 컨테이너를 만들려면
전체 컬렉션을 변환 하려면 STL/CLR 컨테이너를 만들고 컬렉션 생성자에 전달 합니다.
첫 번째 예제는이 절차를 보여 줍니다.
- 또는 -
일반 STL/CLR 컨테이너 만듭니다는 collection_adapter 개체입니다.이 템플릿 클래스는.NET 컬렉션 인터페이스가 인수로 사용 합니다.지 원하는 인터페이스를 확인 하려면 다음을 참조 하십시오. collection_adapter (STL/CLR).
내용을 복사를 합니다.NET 컬렉션의 컨테이너입니다.STL/CLR을 사용 하 여 수행할 수 있습니다 알고리즘, 또는 통해 반복 하는 합니다.NET 컬렉션 및 STL/CLR 컨테이너에 각 요소의 복사본을 삽입 합니다.
두 번째 예제에서는이 절차를 보여 줍니다.
예제
이 예제에서는 제네릭 만들 List<T> 및 5 요소를 추가 합니다.그런 다음 만들는 vector 사용 하는 생성자를 사용 하는 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);
}
}
이 예제에서는 제네릭 만들 Dictionary<TKey, TValue> 및 5 요소를 추가 합니다.다음, 우리가 만들는 collection_adapter 래핑하는 Dictionary<TKey, TValue> 간단한 STL/CLR 컨테이너.마지막으로, 우리가 만들는 map 의 내용을 복사 하 고는 Dictionary<TKey, TValue> 에 map 통해 반복 하 여는 collection_adapter.이 과정에서 우리가 새로운 쌍을 사용 하 여 만들는 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);
}
}
참고 항목
작업
방법: 변환에서 STL/CLR 컨테이너에는.NET 컬렉션