How to convert System::array to std::vector?
Asked Answered
L

2

7

Is there 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);
}
Labuan answered 27/7, 2011 at 15:21 Comment(0)
B
12

Another approach, letting the .NET BCL do the work instead of the C++ standard library:

#include <vector>

void SetLowerBoundsWrapper(array<double>^ lb)
{
    using System::IntPtr;
    using System::Runtime::InteropServices::Marshal;

    std::vector<double> lower(lb->Length);
    Marshal::Copy(lb, 0, IntPtr(&lower[0]), lb->Length);
    _opt->set_lower_bounds(lower);
}

The following both compile for me with VC++ 2010 SP1, and are exactly equivalent:

#include <algorithm>
#include <vector>

void SetLowerBoundsWrapper(array<double>^ lb)
{
    std::vector<double> lower(lb->Length);
    {
        pin_ptr<double> pin(&lb[0]);
        double *first(pin), *last(pin + lb->Length);
        std::copy(first, last, lower.begin());
    }
    _opt->set_lower_bounds(lower);
}

void SetLowerBoundsWrapper2(array<double>^ lb)
{
    std::vector<double> lower(lb->Length);
    {
        pin_ptr<double> pin(&lb[0]);
        std::copy(
            static_cast<double*>(pin),
            static_cast<double*>(pin + lb->Length),
            lower.begin()
        );
    }
    _opt->set_lower_bounds(lower);
}

The artificial scope is to allow the pin_ptr to unpin the memory as early as possible, so as not to hinder the GC.

Banns answered 27/7, 2011 at 15:48 Comment(1)
For the other people out there that are way too busy scrolling in order to read: _opt->set_lower_bounds(lower) is the function that's coming from the question :)Taction
R
1

Do we see any problem with these?

  #include <cstring>
  #include <vector>

  template<typename T>
  cli::array<T> ^ pincpy_a_v(std::vector<T> & v) {
     cli::array<T> ^ a(gcnew cli::array<T>(v.size()));
     pin_ptr<T> a_ptr = &a[0];
     std::memcpy(a_ptr, v.data(), v.size() * sizeof(T)); 
     return a;
  }

  template<typename T>
  std::vector<T> pincpy_v_a(cli::array<T> ^ a) {
     auto v{std::vector<T>(a->Length)};
     pin_ptr<T> a_ptr = &a[0];
     std::memcpy(v.data(), a_ptr, a->Length * sizeof(T)); 
     return v;
  }
Reinertson answered 23/3, 2020 at 9:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.