C++ Cpp/Cli将std::map转换为.net字典

C++ Cpp/Cli将std::map转换为.net字典,c++,dictionary,map,command-line-interface,C++,Dictionary,Map,Command Line Interface,我有一个cpp项目、一个cpp cli项目和一个c#win forms项目。我的原生cpp项目中有一个std::map。如何在cli项目中将其转换为.net dictional?//假定字典为int/int: //Assuming dictionary of int/int: #include <map> #pragma managed using namespace System::Collections::Generic; using namespace std; ///

我有一个cpp项目、一个cpp cli项目和一个c#win forms项目。我的原生cpp项目中有一个std::map。如何在cli项目中将其转换为.net dictional?

//假定字典为int/int:
//Assuming dictionary of int/int:
#include <map>

#pragma managed

using namespace System::Collections::Generic;
using namespace std;

/// <summary>
/// Converts an STL int map keyed on ints to a Dictionary.
/// </summary>
/// <param name="myMap">Pointer to STL map.</param>
/// <returns>Dictionary of int keyed by an int.</returns>
/// <exception cref="ArgumentNullException">The <paramref name="myMap"/> parameter was a NULL pointer.    
Dictionary<int, int>^ Convert(map<int, int>* myMap)
{ 
  if (!myMap)
    throw gcnew System::ArgumentNullException("myMap");

  Dictionary<int, int>^ h_result = gcnew Dictionary<int, int>(myMap->size());

  for (pair<int, int> kvp : *myMap)
  {
     h_result->Add(kvp.first, kvp.second);
  }

  return h_result;
}
#包括 #布拉格管理 使用命名空间System::Collections::Generic; 使用名称空间std; /// ///将键入ints的STL int映射转换为字典。 /// ///指向STL映射的指针。 ///由整数键控的整数字典。 ///该参数是空指针。 字典^Convert(map*myMap) { 如果(!myMap) 抛出新系统::ArgumentNullException(“myMap”); Dictionary^h_result=gcnewdictionary(myMap->size()); 对于(配对kvp:*myMap) { h_结果->添加(kvp.first,kvp.second); } 返回h_结果; }
//假定int/int字典:
#包括
#布拉格管理
使用命名空间System::Collections::Generic;
使用名称空间std;
/// 
///将键入ints的STL int映射转换为字典。
/// 
///指向STL映射的指针。
///由整数键控的整数字典。
///该参数是空指针。
字典^Convert(map*myMap)
{ 
如果(!myMap)
抛出新系统::ArgumentNullException(“myMap”);
Dictionary^h_result=gcnewdictionary(myMap->size());
对于(配对kvp:*myMap)
{
h_结果->添加(kvp.first,kvp.second);
}
返回h_结果;
}

您想做什么?你是怎么做到的?怎么不起作用?不,我没试过。我想知道是否有一个简单的方法。你想做什么?你是怎么做到的?怎么不起作用?不,我没试过。我想知道是否有一个简单的方法。