Cómo: Convierte de un contenedor de STL/CLR a una colección de .NET

En este tema se muestra cómo convertir los contenedores de STL/CLR a las colecciones equivalentes de .NET.Como ejemplo, mostramos cómo convertir un STL/CLR vector a .NET ICollection<T> y cómo convertir un STL/CLR mapa a .NET IDictionary<TKey, TValue>, pero el procedimiento es similar para todas las colecciones y contenedores.

Para crear una colección de un contenedor

  • Utilice uno de los métodos siguientes:

    • Para convertir la parte de un contenedor, llamar a la función de make_collection , y pasar el iterador de inicio y el iterador de final del contenedor de STL/CLR se copie en la colección de .NET.Esta función de plantilla toma un iterador de STL/CLR como argumento de plantilla.El primer ejemplo se muestra este método.

    • Para convertir un contenedor completo, convierta el contenedor a una colección adecuada de la interfaz de intercalación o interfaz.NET.El segundo ejemplo se muestra este método.

Ejemplo

En este ejemplo, creamos un STL/CLR vector y agregamos 5 elementos a.A continuación, creamos una colección .NET llamando a la función de make_collection .Finalmente, se muestra el contenido de la colección recién creada.

// 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);
    }
}
  

En este ejemplo, creamos un STL/CLR map y agregamos 5 elementos a.A continuación, creamos .NET IDictionary<TKey, TValue> y asignar map directamente al.Finalmente, se muestra el contenido de la colección recién creada.

// 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);
    }
}
  

Vea también

Tareas

Cómo: Convertir de una colección de .NET en un contenedor de STL/CLR

Referencia

range_adapter (STL/CLR)

Otros recursos

Referencia de la biblioteca de STL/CLR