What is the most efficient way to convert a std::vector to a .NET List?
To give some context, I am wrapping an unmanaged C++ class with C++/CLI. The C++/CLI class holds a pointer to the C++ class and I have a wrapper for each public method.
One method returns a std::vector, so in my wrapper I was going to return the .NET class List. I.e.
// unmanaged class
class A
{
public:
std::vector<int> runList();
}
// managed class
public ref class A
{
public:
// the below is obviously extremely inefficient
List<UInt32> MethodA()
{
std::vector<unsigned int> runList = mpChannelNode->runList();
std::vector<unsigned int>::iterator itr;
List<UInt32> list = gcnew List<UInt32>();
for (itr = runList.begin(); itr != runList.end(); itr++)
{
list.Add(*itr);
}
return list;
}
private:
A* mpChannelNode;
}
How can I make this more efficient? Feel free to recommend a different return type for the .NET class. Lets assume I just need to get that vector into managed world efficiently in any shape or form.
vector<T>
, why not implementIList<T>
and proxy all of the operations to thevector
? – Gabe