방법: 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);
}
}
The contents of the System::Collections::Generic::ICollection are:
3
5
7
이 예제에서는 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);
}
}
The contents of the IDictionary 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 라이브러리 참조
방법: .NET 컬렉션에서 STL/CLR 컨테이너로 변환
range_adapter(STL/CLR)
피드백
https://aka.ms/ContentUserFeedback
출시 예정: 2024년 내내 콘텐츠에 대한 피드백 메커니즘으로 GitHub 문제를 단계적으로 폐지하고 이를 새로운 피드백 시스템으로 바꿀 예정입니다. 자세한 내용은 다음을 참조하세요.다음에 대한 사용자 의견 제출 및 보기