Can one elegantly convert std: vector to cliext :: vector or cli :: array <T>?

How's that for a catchy headline?

I need to convert back and forth from a CLR-compatible type, such as an array, and of type std :: vector.

Are there any adapters, or should I just copy it every time I call one of my own methods?

There are some interesting methods for converting between variants of the CLI STE class and CLR types, but I don’t understand how to get a standard vector into STL types without the following loop.

This is what I do throughout 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();
+3
source share
3 answers

, " ", ?

-

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 , , , zillion copy.

+4
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;
}
0

You can try the following:

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);
}
Run code
0
source

Source: https://habr.com/ru/post/1762979/


All Articles