Does anyone know of any easy way to convert a CLI/.NET System::array to a C++ std::vector, besides doing it element-wise?
I'm writing a wrapper method (SetLowerBoundsWrapper, below) in CLI/C++ that accepts a System::array as an argument, and passes the equivalent std::vector to a native C++ method (set_lower_bounds). Currently I do this as follows:
using namespace System;
void SetLowerBoundsWrapper(array<double>^ lb)
{
int n = lb->Length;
std::vector<double> lower(n); //create a std::vector
for(int i = 0; i<n ; i++)
{
lower[i] = lb[i]; //copy element-wise
}
_opt->set_lower_bounds(lower);
}
Another approach, letting the .NET BCL do the work instead of the C++ standard library:
EDIT (in response to comments on Konrad's answer):
The following both compile for me with VC++ 2010 SP1, and are exactly equivalent:
(The artificial scope is to allow the
pin_ptr
to unpin the memory as early as possible, so as not to hinder the GC.)