将C ++ / CLI字符串数组转换为字符串向量 [英] Convert C++/CLI String Array into a vector of strings
问题描述
我在C ++ / CLI中有一个参数,如下所示:
I have a parameter in C++/CLI as follows:
array<String^>^ list
我希望能够将其转换为字符串向量。
I want to be able to convert this into a vector of strings.
我将如何去做?
推荐答案
MSDN 提供了有关如何封送数据的一些详细信息。它们还为 msclr :: marshal_as
w.r.t提供了一些标准实现。 std :: string
。
MSDN provides some detail on how to marshal data. They also provide some standard implementation for msclr::marshal_as
w.r.t. std::string
.
cli :: array
稍微复杂一点,这里一般情况的关键是先 pin
数组(这样我们才不会在后面移动它)。对于 String ^
转换, marshal_as
将 pin
适当的 String
。
The cli::array
is a little more complex, the key for the general case here is to pin
the array first (so that we don't have it moving behind our backs). In the case of the String^
conversion, the marshal_as
will pin
the String
appropriately.
代码的要旨是:
vector<string> marshal_array(cli::array<String^>^ const& src)
{
vector<std::string> result(src->Length);
if (src->Length) {
cli::pin_ptr<String^> pinned = &src[0]; // general case
for (int i = 0; i < src->Length; ++i) {
result[static_cast<size_t>(i)] = marshal_as<string>(src[i]);
}
}
return result;
}
这篇关于将C ++ / CLI字符串数组转换为字符串向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!