可以优雅地将 std:vector 转换为 cliext::vector 或 cli::array吗?

发布于 2024-09-17 07:13:39 字数 631 浏览 7 评论 0原文

一个吸引人的标题怎么样?

我需要在 CLR 兼容类型(如数组)和 std::vector 类型之间来回转换。

是否有任何适配器方法,或者我应该在每次调用我的本机方法之一时继续将其复制进复制出?

有一些有趣的方法可以在 cliext STL 变体类和 CLR 类型之间进行转换,但我不知道如何在没有 for next 循环的情况下将标准向量转换为 STL 类型。

这就是我在这个项目中到处做的事情:

vector<double> galilVector = _galilClass->arrayUpload(marshal_as<string>(arrayName));
List<double>^ arrayList = gcnew List<double>();

// Copy out the vector into a list for export to .net
for(vector<double>::size_type i = 0; i < galilVector.size(); i++) 
{
    arrayList->Add(galilVector[i]);
}

return arrayList->ToArray();

How's that for a catchy title?

I need to convert back and forth from a CLR compliant type, like an array, and a std::vector type.

Are there any adapter methods out there, or should I just keep copying it in and out each time I call one of my native methods?

There are some interesting methods for converting between the cliext STL variant classes and CLR types, but I'm at a loss for how to get the standard vector into the STL types without a for next loop.

This is what I'm doing all over the place in this project:

vector<double> galilVector = _galilClass->arrayUpload(marshal_as<string>(arrayName));
List<double>^ arrayList = gcnew List<double>();

// Copy out the vector into a list for export to .net
for(vector<double>::size_type i = 0; i < galilVector.size(); i++) 
{
    arrayList->Add(galilVector[i]);
}

return arrayList->ToArray();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

鹤舞 2024-09-24 07:13:39

与其“到处这样做”,为什么不将该逻辑变成可重用的函数呢?

记得

template<typename T>
generic<typename S>
std::vector<T> marshal_as(System::Collections::Generic::ICollection<S>^ list)
{
  if (list == nullptr) throw gcnew ArgumentNullException(L"list");
  std::vector<T> result;
  result.reserve(list->Count);
  for each (S& elem in list)
    result.push_back(marshal_as<T>(elem));
  return result;
}

使用向量的 swap 成员函数将元素快速移动到你想要保存它们的向量中,如果你只是赋值,那么将会调用无数的复制构造函数。

Instead of "doing that all over the place", why don't you make that logic into a reusable function?

Something like

template<typename T>
generic<typename S>
std::vector<T> marshal_as(System::Collections::Generic::ICollection<S>^ list)
{
  if (list == nullptr) throw gcnew ArgumentNullException(L"list");
  std::vector<T> result;
  result.reserve(list->Count);
  for each (S& elem in list)
    result.push_back(marshal_as<T>(elem));
  return result;
}

Remember to use vector's swap member function to quickly move the elements into the vector you want to hold them, if you just assign then a zillion copy constructors would be called.

君勿笑 2024-09-24 07:13:39
IList<int>^ Loader::Load(int id)
{
    vector<int> items;
    m_LoaderHandle->Loader->Load(id, items);

    cliext::vector<int> ^result = gcnew cliext::vector<int>(items.size());
    cliext::copy(items.begin(), items.end(), result->begin());

    return result;
}
IList<int>^ Loader::Load(int id)
{
    vector<int> items;
    m_LoaderHandle->Loader->Load(id, items);

    cliext::vector<int> ^result = gcnew cliext::vector<int>(items.size());
    cliext::copy(items.begin(), items.end(), result->begin());

    return result;
}
尾戒 2024-09-24 07:13:39

你可以试试这个:

cliext::vector<Single> vec_cliext; 
					
std::vector<float> vec_std;

cliext::vector<Single>::iterator it = vec_cliext.begin(); 
for (; it != vec_cliext.end(); ++it)
{
	float temp = *it;
	vec_std.push_back(temp);
}

You can try this:

cliext::vector<Single> vec_cliext; 
					
std::vector<float> vec_std;

cliext::vector<Single>::iterator it = vec_cliext.begin(); 
for (; it != vec_cliext.end(); ++it)
{
	float temp = *it;
	vec_std.push_back(temp);
}

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