可以优雅地将 std:vector 转换为 cliext::vector 或 cli::array吗?
一个吸引人的标题怎么样?
我需要在 CLR 兼容类型(如数组)和 std::vector 类型之间来回转换。
是否有任何适配器方法,或者我应该在每次调用我的本机方法之一时继续将其复制进复制出?
有一些有趣的方法可以在 cliext STL 变体类和 CLR 类型之间进行转换,但我不知道如何在没有 for next 循环的情况下将标准向量转换为 STL 类型。
这就是我在这个项目中到处做的事情:
vector<double> galilVector = _galilClass->arrayUpload(marshal_as<string>(arrayName));
List<double>^ arrayList = gcnew List<double>();
// Copy out the vector into a list for export to .net
for(vector<double>::size_type i = 0; i < galilVector.size(); i++)
{
arrayList->Add(galilVector[i]);
}
return arrayList->ToArray();
How's that for a catchy title?
I need to convert back and forth from a CLR compliant type, like an array, and a std::vector type.
Are there any adapter methods out there, or should I just keep copying it in and out each time I call one of my native methods?
There are some interesting methods for converting between the cliext STL variant classes and CLR types, but I'm at a loss for how to get the standard vector into the STL types without a for next loop.
This is what I'm doing all over the place in this project:
vector<double> galilVector = _galilClass->arrayUpload(marshal_as<string>(arrayName));
List<double>^ arrayList = gcnew List<double>();
// Copy out the vector into a list for export to .net
for(vector<double>::size_type i = 0; i < galilVector.size(); i++)
{
arrayList->Add(galilVector[i]);
}
return arrayList->ToArray();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
与其“到处这样做”,为什么不将该逻辑变成可重用的函数呢?
记得
使用向量的
swap
成员函数将元素快速移动到你想要保存它们的向量中,如果你只是赋值,那么将会调用无数的复制构造函数。Instead of "doing that all over the place", why don't you make that logic into a reusable function?
Something like
Remember to use vector's
swap
member function to quickly move the elements into the vector you want to hold them, if you just assign then a zillion copy constructors would be called.你可以试试这个:
You can try this: