列表和字符串转换
我需要在 C++/CLI 中有一个函数,它可以在 C++ 本机 std::list
和 C# 中的 string[]
之间建立链接用我的 WinForm 做类似的事情:
ComboBox1.Items.AddRange(installs);
installs 是string[]
。
你有主意吗?我该怎么做?如果没有 Intellisense,C++/CLI 编程就会很困难。 :(
你对此有何看法?
本机 C++ .cpp
std::list<std::string>* Get_Liste_place_de_marche(void)
{
list<string>* liste_place_de_marche = new list<string>;
liste_place_de_marche->push_back("CAC 40");
liste_place_de_marche->push_back("DAX");
return liste_place_de_marche;
}
我需要使用顶部的最后一个代码来编写此函数:
C++/CLI .cpp 在我的 Winform 中用 C# 调用
array<System::String^>^ NativeMethod::Get_Liste_place_de_marche(void)
{
typedef std::list<std::string>::const_iterator iter_t;
std::list<std::string> const* list = new std::list<std::string>;
list = ::Get_Liste_place_de_marche();
array<System::String^>^ ret = gcnew array<System::String^>(list->size());
int j = 0;
for (iter_t i = list->begin(); i != list->end(); ++i)
ret[j++] = gcnew System::String(i->c_str());
return ret;
}
它应该可以工作?因为我有很多错误.. 。
I need to have a function in C++/CLI which do the link between a C++ native std::list<std::string>
and a string[]
in C# to do something like that with my WinForm :
ComboBox1.Items.AddRange(installs);
installs is the string[]
.
Do you have an idea? How can I do this? C++/CLI programming is hard without Intellisense. :(
What do you think about this?
Native C++ .cpp
std::list<std::string>* Get_Liste_place_de_marche(void)
{
list<string>* liste_place_de_marche = new list<string>;
liste_place_de_marche->push_back("CAC 40");
liste_place_de_marche->push_back("DAX");
return liste_place_de_marche;
}
And I need to code this function using the last code to the top :
C++/CLI .cpp called in my Winform with C#
array<System::String^>^ NativeMethod::Get_Liste_place_de_marche(void)
{
typedef std::list<std::string>::const_iterator iter_t;
std::list<std::string> const* list = new std::list<std::string>;
list = ::Get_Liste_place_de_marche();
array<System::String^>^ ret = gcnew array<System::String^>(list->size());
int j = 0;
for (iter_t i = list->begin(); i != list->end(); ++i)
ret[j++] = gcnew System::String(i->c_str());
return ret;
}
It should work? Because I have many errors...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以下内容应该可以完成这项工作:
不过,我会尽力使其更笼统。例如,C++ 中通常使用迭代器范围而不是容器。此外,以上仅适用于(零终止)字符串。转换其他对象集合需要非常类似的代码。将对象转换抽象出来可能是有意义的。
The following should do the job:
I would try to keep this more general, though. For instance, it’s customary in C++ to work on iterator ranges instead of containers. Furthermore, the above only works on (zero-terminated) strings. Very similar code will be needed to convert other object collections. It might make sense to abstract the object conversion away.