[英]Convert array<String^>^ to std::vector<std::string>
How can I convert a managed array<String^>^
to std::vector<std::string>
. 如何将托管
array<String^>^
为std::vector<std::string>
。 The following code doesnt compile and I can't figure out why. 以下代码无法编译,我也不知道为什么。 Funny enough, if I use an int for example then it works fine.
有趣的是,如果我使用int为例,则可以正常工作。 thanks in advance:
提前致谢:
std::vector<std::string> ConvertToUnManaged(array<String^>^ ar)
{
using System::IntPtr;
using System::Runtime::InteropServices::Marshal;
std::vector<std::string> vector(ar->Length);
Marshal::Copy(ar, 0, IntPtr(&vector[0]), ar->Length);
return vector;
}
There is no overload of Marshall::Copy
with array of String parameter. 没有
Marshall::Copy
的String参数数组重载。
Iterating over the array element and converting one-by-one work. 遍历数组元素并进行一对一转换。
#include <msclr/marshal_cppstd.h>
std::vector<std::string> ConvertToUnManaged(array<String^>^ ar)
{
std::vector<std::string> vector(ar->Length);
for (int i = 0; i < ar->Length; ++i) {
auto s = ar[i];
vector[i] = msclr::interop::marshal_as<std::string>(s);
}
return vector;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.