Cpp/Cli将std :: map转换为.net词典 [英] Cpp/Cli Convert std::map to .net dictionary
本文介绍了Cpp/Cli将std :: map转换为.net词典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个cpp项目,一个cpp cli项目和一个c#win表单项目.我的本机cpp项目中有一个std :: map.如何在cli项目中将其转换为.net字典?
I have a cpp project, a cpp cli project and a c# win forms project. I have a std::map in my native cpp project. How can i convert it to .net dictonary in my cli project?
推荐答案
//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;
}
这篇关于Cpp/Cli将std :: map转换为.net词典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文