我想将 Platform::Array
对象中的一系列项目复制到另一个 Platform::Array
。我当然可以解决这个问题,例如使用 for
循环:
int srcIdx = srcIdx0;
int destIdx = destIdx0;
for (int i = 0; i < count; ++i, ++srcIdx, ++destIdx)
dest[destIdx] = src[srcIdx];
我想知道的是,C++/CX(组件扩展)中是否有一些内置功能可以更高效、更简洁地执行此操作?
在 C# 中,有 Array.Copy方法,并使用 C++/CLI Marshal.Copy至少对于复制“原始”类型来说是一个选择。
在 C++ STL 中,有 std::copy
和 std::copy_n
,但据我所知,这些算法不适用于 平台: :Array
“迭代器” begin()
和 end()
。
是否有一个“隐藏”在某处的 C++/CX 便捷复制方法,或者我是否必须为该操作回退到显式 for
循环?
最佳答案
此时似乎没有内置的Platform::Array
复制方法,所以我为此实现了自己的模板函数:
template<typename T> void Copy(
const Platform::Array<T>^ sourceArray,
int sourceIndex,
Platform::Array<T>^ destinationArray,
int destinationIndex,
int length)
{
for (int i = 0; i < length; ++i, ++sourceIndex, ++destinationIndex)
destinationArray[destinationIndex] = sourceArray[sourceIndex];
};
非常欢迎关于如何改进复制部分的建议:-)
关于c++ - 两个 C++/CX Platform::Array:s 之间的高效复制方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14724668/