방법: 변환에서 STL/CLR 컨테이너에는.NET 컬렉션
이 항목에 동등한 STL/CLR 컨테이너로 변환 하는 방법을 보여 줍니다.NET 컬렉션입니다.예를 들어 우리는 STL/CLR을 변환 하는 방법을 표시 합니다. 벡터 에.NET ICollection<T> 및 STL/CLR을 변환 하는 방법을 맵 에.NET IDictionary<TKey, TValue>, 하지만 모든 컬렉션 및 컨테이너에 대 한 절차는 유사 합니다.
컨테이너에서 컬렉션을 만들려면
다음 방법 중 하나를 사용합니다.
컨테이너의 일부를 변환 하려면 호출을 make_collection 작동을 하 고 반복기를 시작 및 끝 반복기는 STL/CLR 컨테이너를 복사할 전달의.NET 컬렉션입니다.템플릿 함수 STL/CLR 반복기는 템플릿 인수로 사용합니다.첫 번째 예제에서는이 메서드를 보여 줍니다.
전체 컨테이너를 변환 하려면 해당 컨테이너에 캐스팅 합니다.NET 컬렉션 인터페이스 또는 인터페이스 컬렉션입니다.두 번째 예제에서는이 메서드를 보여 줍니다.
예제
이 예제에서는 STL/CLR 만들 vector 및 5 요소를 추가 합니다.그런 다음 만들는.호출 하 여 NET 컬렉션의 make_collection 함수.마지막으로 우리가 새로 만든된 컬렉션의 내용을 표시 합니다.
// cliext_convert_vector_to_icollection.cpp
// compile with: /clr
#include <cliext/adapter>
#include <cliext/vector>
using namespace cliext;
using namespace System;
using namespace System::Collections::Generic;
int main(array<System::String ^> ^args)
{
cliext::vector<int> primeNumbersCont;
primeNumbersCont.push_back(2);
primeNumbersCont.push_back(3);
primeNumbersCont.push_back(5);
primeNumbersCont.push_back(7);
primeNumbersCont.push_back(11);
System::Collections::Generic::ICollection<int> ^iColl =
make_collection<cliext::vector<int>::iterator>(
primeNumbersCont.begin() + 1,
primeNumbersCont.end() - 1);
Console::WriteLine("The contents of the System::Collections::Generic::ICollection are:");
for each (int i in iColl)
{
Console::WriteLine(i);
}
}
이 예제에서는 STL/CLR 만들 map 및 5 요소를 추가 합니다.그런 다음 만들는.NET IDictionary<TKey, TValue> 및 할당은 map 직접 합니다.마지막으로 우리가 새로 만든된 컬렉션의 내용을 표시 합니다.
// cliext_convert_map_to_idictionary.cpp
// compile with: /clr
#include <cliext/adapter>
#include <cliext/map>
using namespace cliext;
using namespace System;
using namespace System::Collections::Generic;
int main(array<System::String ^> ^args)
{
cliext::map<float, int> ^aMap = gcnew cliext::map<float, int>;
aMap->insert(cliext::make_pair<float, int>(42.0, 42));
aMap->insert(cliext::make_pair<float, int>(13.0, 13));
aMap->insert(cliext::make_pair<float, int>(74.0, 74));
aMap->insert(cliext::make_pair<float, int>(22.0, 22));
aMap->insert(cliext::make_pair<float, int>(0.0, 0));
System::Collections::Generic::IDictionary<float, int> ^iDict = aMap;
Console::WriteLine("The contents of the IDictionary are:");
for each (KeyValuePair<float, int> ^kvp in iDict)
{
Console::WriteLine("Key: {0:F} Value: {1}", kvp->Key, kvp->Value);
}
}