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