Nota:
El acceso a esta página requiere autorización. Puede intentar iniciar sesión o cambiar directorios.
El acceso a esta página requiere autorización. Puede intentar cambiar los directorios.
En este tema se muestra cómo convertir colecciones.NET a sus contenedores de equivalente STL/CLR.Como ejemplo muestran cómo convertir .NET List<T> un STL/CLR vector y cómo convertir .NET Dictionary<TKey, TValue> un STL/CLR mapa, pero el procedimiento es similar para todas las colecciones y contenedores.
Para crear un contenedor de una colección
Para convertir una colección completa, crear un contenedor de STL/CLR y pase la colección al constructor.
El primer ejemplo se muestra este procedimiento.
O bien
Cree un contenedor genérico de STL/CLR creando un objeto de collection_adapter .Esta clase de plantilla toma una interfaz de intercalación de .NET como argumento.Para comprobar si se admiten las interfaces, vea collection_adapter (STL/CLR).
Copie el contenido de la colección.NET al contenedor.Puede hacerlo con un STL/CLR algoritmo, o itera en la colección de .NET y insertando una copia de cada elemento en el contenedor de STL/CLR.
El segundo ejemplo se muestra este procedimiento.
Ejemplo
En este ejemplo, creamos List<T> genérico y agregamos 5 elementos a.A continuación, creamos vector mediante el constructor que toma IEnumerable<T> como argumento.
// 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);
}
}
En este ejemplo, creamos Dictionary<TKey, TValue> genérico y agregamos 5 elementos a.A continuación, creamos collection_adapter para ajustar Dictionary<TKey, TValue> como contenedor simple de STL/CLR.Finalmente, creamos map y copiamos el contenido de Dictionary<TKey, TValue> a map itera en collection_adapter.Durante este proceso, crea un nuevo par mediante la función de make_pair , y insertamos nuevos pares directamente en 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);
}
}
Vea también
Tareas
Cómo: Convierte de un contenedor de STL/CLR a una colección de .NET