What is the most efficient way to convert a std::vector to a .NET List?

前端 未结 3 1129
情歌与酒
情歌与酒 2021-02-14 01:38

What is the most efficient way to convert a std::vector to a .NET List?

To give some context, I am wrapping an unmanaged C++ class with C++/CLI. The C++/CLI class holds

3条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-14 02:20

    If you're really that concerned about it, use unverifiable code instead:

    List^ MethodA()
    {
        std::vector const& runList = mpChannelNode->runList();
        array^ ret = gcnew array(runList.size());
        if (runList.size())
        {
            pin_ptr dest = &ret[0];
            std::memcpy(dest, &runList[0], runList.size() * sizeof(unsigned));
        }
        return gcnew List(ret);
    }
    

    That said, I'd be surprised if there was a noticeable difference either way...

提交回复
热议问题