【问题标题】:Cpp/Cli Convert std::map to .net dictionaryCpp/Cli 将 std::map 转换为 .net 字典
【发布时间】:2012-03-29 07:32:42
【问题描述】:

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

【问题讨论】:

  • 你想做什么?你是如何尝试的?怎么没用?
  • 不,我没试过。我想知道是否有一个简单的方法。

标签: c++ dictionary map command-line-interface


【解决方案1】:
//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;
}

【讨论】:

    猜你喜欢
    • 2019-07-21
    • 1970-01-01
    • 2018-10-03
    • 2018-11-06
    • 1970-01-01
    • 2010-09-10
    • 1970-01-01
    • 2017-12-11
    • 2020-07-03
    相关资源
    最近更新 更多