列表和字符串转换

发布于 2024-11-17 20:39:00 字数 1297 浏览 0 评论 0原文

我需要在 C++/CLI 中有一个函数,它可以在 C++ 本机 std::list 和 C# 中的 string[] 之间建立链接用我的 WinForm 做类似的事情:

    ComboBox1.Items.AddRange(installs);

installsstring[]

你有主意吗?我该怎么做?如果没有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

温柔女人霸气范 2024-11-24 20:39:00

以下内容应该可以完成这项工作:

array<System::String^>^ ArrayFromList(std::list<std::string> const& list) {
    typedef std::list<std::string>::const_iterator iter_t;

    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;
}

不过,我会尽力使其更笼统。例如,C++ 中通常使用迭代器范围而不是容器。此外,以上仅适用于(零终止)字符串。转换其他对象集合需要非常类似的代码。将对象转换抽象出来可能是有意义的。

The following should do the job:

array<System::String^>^ ArrayFromList(std::list<std::string> const& list) {
    typedef std::list<std::string>::const_iterator iter_t;

    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 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文