Comment : convertir un conteneur STL/CLR en collection .NET

Cette rubrique montre comment convertir des conteneurs STL/CLR en collections .NET équivalentes. Par exemple, nous montrons comment convertir un vecteur STL/CLR en .NET ICollection<T> et comment convertir un mappage STL/CLR en .NETIDictionary<TKey,TValue>, mais la procédure est similaire pour toutes les collections et conteneurs.

Pour créer une collection à partir d’un conteneur

  1. Suivez l’une des méthodes suivantes :

    • Pour convertir une partie d’un conteneur, appelez la fonction make_collection et passez l’itérateur de début et l’itérateur de fin du conteneur STL/CLR à copier dans la collection .NET. Ce modèle de fonction prend un itérateur STL/CLR en tant qu’argument de modèle. Le premier exemple illustre cette méthode.

    • Pour convertir un conteneur entier, convertissez le conteneur en une interface de collection ou une collection d’interface .NET appropriée. Le deuxième exemple illustre cette méthode.

Exemples

Dans cet exemple, nous créons un STL/CLR vector et y ajoutons 5 éléments. Ensuite, nous créons une collection .NET en appelant la make_collection fonction. Enfin, nous affichons le contenu de la collection nouvellement créée.

// 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

Dans cet exemple, nous créons un STL/CLR map et y ajoutons 5 éléments. Ensuite, nous créons un .NET IDictionary<TKey,TValue> et lui assignons directement map . Enfin, nous affichons le contenu de la collection nouvellement créée.

// 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

Voir aussi

Référence de bibliothèque STL/CLR
Guide pratique pour convertir une collection .NET en conteneur STL/CLR
range_adapter (STL/CLR)