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

En este tema se muestra cómo convertir contenedores STL/CLR en sus colecciones de .NET equivalentes. Por ejemplo, se muestra cómo convertir un vector de STL/CLR en .NET ICollection<T> y cómo convertir una asignación de STL/CLR en .NET IDictionary<TKey,TValue>, pero el procedimiento es similar para todas las colecciones y contenedores.

Para crear una colección a partir de un contenedor

  1. Utilice uno de los métodos siguientes:

    • Para convertir parte de un contenedor, llame a la función make_collection y pase el iterador inicial y el iterador final del contenedor STL/CLR que se va a copiar en la colección de .NET. Esta plantilla de función toma un iterador STL/CLR como argumento de plantilla. En el primer ejemplo se demuestra este método.

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

Ejemplos

En este ejemplo, se crea un vector de STL/CLR y se le agregan 5 elementos. A continuación, se crea una colección de .NET mediante una llamada a la función make_collection. Por último, 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);
    }
}
The contents of the System::Collections::Generic::ICollection are:
3
5
7

En este ejemplo, se crea un map de STL/CLR y se le agregan 5 elementos. A continuación, se crea un IDictionary<TKey,TValue> de .NET y se le asigna map directamente. Por último, 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);
    }
}
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

Consulte también

Referencia de la biblioteca STL/CLR
Cómo: Convertir una colección .NET en un contenedor STL/CLR
range_adapter (STL/CLR)