Set*_*eth 10 .net c++-cli visual-c++
将std :: vector转换为.NET List的最有效方法是什么?
为了给出一些上下文,我用C++/CLI包装一个非托管的C++类.C++/CLI类包含一个指向C++类的指针,我有一个每个公共方法的包装器.
一个方法返回一个std :: vector,所以在我的包装器中我将返回.NET类List.即
// unmanaged class
class A
{
public:
std::vector<int> runList();
}
// managed class
public ref class A
{
public:
// the below is obviously extremely inefficient
List<UInt32> MethodA()
{
std::vector<unsigned int> runList = mpChannelNode->runList();
std::vector<unsigned int>::iterator itr;
List<UInt32> list = gcnew List<UInt32>();
for (itr = runList.begin(); itr != runList.end(); itr++)
{
list.Add(*itr);
}
return list;
}
private:
A* mpChannelNode;
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能提高效率呢?随意为.NET类推荐不同的返回类型.让我们假设我只需要以任何形式或形式有效地将该向量导入托管世界.
如果您真的关心它,请使用无法验证的代码:
List<unsigned>^ MethodA()
{
std::vector<unsigned> const& runList = mpChannelNode->runList();
array<unsigned>^ ret = gcnew array<unsigned>(runList.size());
if (runList.size())
{
pin_ptr<unsigned> dest = &ret[0];
std::memcpy(dest, &runList[0], runList.size() * sizeof(unsigned));
}
return gcnew List<unsigned>(ret);
}
Run Code Online (Sandbox Code Playgroud)
也就是说,如果两种方式之间存在显着差异,我会感到惊讶......
| 归档时间: |
|
| 查看次数: |
4948 次 |
| 最近记录: |