How do I make a clone of a Windows Runtime vector in C++/CX?

Raymond Chen

Raymond

There are still some people maintaining code bases written in C++/CX, even though C++/WinRT is the new hotness. Suppose you have a reference to a Windows Runtime vector in C++/CX, either an IVector<T>^ or an IVector­View<T>^, and you want to clone it so that you can operate on the clone without affecting the original.

You could create a vector and copy the items across:

IVector<Thing^>^ original = GetTheThings();
Vector<Thing^> vec = ref new Vector<Thing^>();
for (auto&& thing : original)
{
  vec->Append(thing);
}

You can make the Vector run the loop by using the constructor overload that takes a pair of iterators.

IVector<Thing^>^ original = GetTheThings();
Vector<Thing^> vec = ref new Vector<Thing^>(begin(original), end(original));

Even more directly, you could slurp the entire collection into a std::vector and then move the std::vector into a new Platform::Collections::Vector.

IVector<Thing^>^ original = GetTheThings();
Vector<Thing^> vec = ref new Vector<Thing^>(to_vector(original));

 

Raymond Chen
Raymond Chen

Follow Raymond   

1 comment

Comments are closed.