方法: .NET コレクションを STL/CLR コンテナーに変換する

このトピックでは、.NET のコレクションを同等の STL/CLR のコンテナーに変換する方法について説明します。 例として、.NET の List<T> を STL/CLR の vector に変換する方法と、.NET の Dictionary<TKey,TValue> を STL/CLR の map に変換する方法を示しますが、その手順はすべてのコレクションやコンテナーで類似しています。

コレクションからコンテナーを作成するには

  1. コレクション全体を変換するには、STL/CLR コンテナーを作成し、そのコレクションをコンストラクターに渡します。

    最初の例で、この手順を示します。

- または -

  1. collection_adapter オブジェクトを作成することで、汎用の STL/CLR コンテナーを作成します。 このテンプレート クラスでは、引数として .NET コレクション インターフェイスを受け取ります。 サポートされているインターフェイスは、「collection_adapter (STL/CLR)」で確認してください。

  2. .NET コレクションの内容をコンテナーにコピーします。 これは、STL/CLR の algorithm を使用するか、.NET コレクションを反復処理し、各要素のコピーを STL/CLR コンテナーに挿入することで行うことができます。

    2 番目の例で、この手順を示します。

この例では、汎用の List<T> を作成し、それに 5 つの要素を追加します。 その後、引数として IEnumerable<T> を受け取るコンストラクターを使用して、vector を作成します。

// 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);
    }
}
The contents of the cliext::vector are:
2
3
5
7
11

この例では、汎用の Dictionary<TKey,TValue> を作成し、それに 5 つの要素を追加します。 その後、collection_adapter を作成して Dictionary<TKey,TValue> を単純な STL/CLR コンテナーとしてラップします。 最後に、map を作成し、collection_adapter を反復処理することで、Dictionary<TKey,TValue> の内容を map にコピーします。 このプロセス中に、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);
    }
}
The contents of the cliext::map 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

関連項目

STL/CLR ライブラリ リファレンス
adapter (STL/CLR)
方法: STL/CLR コンテナーを .NET コレクションに変換する