如何:从 .NET 集合转换为 STL/CLR 容器
本主题显示如何将 .NET 集合到等效。STL/CLR 容器 举例来说我们演示如何转换 List .NET。STL/CLR 矢量 和如何将 .NET Dictionary。STL/CLR 映射,但过程为所有集合与容器类似。
创建从集合的容器
若要将整个集合,请创建并将该集合传递给构造函数。STL/CLR 容器
第一个示例演示此过程。
- 或 -
通过 collection_adapter 创建对象创建泛型容器。STL/CLR 此模板类采用 .NET 集合接口作为参数。 若要验证的接口支持,请参见 collection_adapter (STL/CLR)。
将 .NET 集合内容的容器。 使用 算法STL/CLR,可以完成,或由循环访问 .NET 集合和插入每个元素重复到 STL/CLR 容器。
第二个示例演示此过程。
示例
在此示例中,我们创建泛型 List 并添加元素 5 到它。 然后,我们创建 vector 使用将 IEnumerable 用作参数的构造函数。
// 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 并添加元素 5 到它。 然后,我们创建一个 collection_adapter 包装 Dictionary 作为简单 STL/CLR 的容器。 最后,我们创建 map 并将 Dictionary 的内容设置通过 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);
}
}