How to create a function equal to Array.Copy from C # in C ++

6

I would like to create a function similar to the Array.Copy of C # in C ++.

This function has the prototype like this:

public static void Copy(
    Array sourceArray,
    int sourceIndex,
    Array destinationArray,
    int destinationIndex,
    int length
);

And I would like to do a function that would do the same function in C ++.

I tried to do using for , but I could not make it work with destinationIndex .

My code:

template <T> void Buffer <T> ::copy(T * src, int indexSrc, T * dst, int indexDst, int elements) {
    for (int srcAux = indexSrc, dstAux = indexDst, int aux = 0; aux != elements; srcAux++, dstAux++, aux++) {
        dst[indexDst] = src[srcAux];
    }
}
    
asked by anonymous 28.01.2016 / 17:47

1 answer

6

C ++ already has something ready to do this with std::copy() :

std::copy(std::begin(src), std::end(src), std::begin(dest));

Trying to translate the code from one language to another without understanding the language does not work. Each language has its own specificity and way of doing each operation.

Our tag

28.01.2016 / 19:11